Serilog Sink to ASPNET Core Logging

I wish I could process my code using Serilog and Microsoft.Extensions.Logging (ILogger). And let them both be configured the same way, so they write the same "sinks". I have an ASPNET Core application with running logs and some dependent libraries using Serilog that I would like to get from the log output (in the same place as the ASPNET Core application).

I know that there is a nuget package for using Serilog registration with ASPNET Core applications. However this is a private extension

(WebAppBuilder).UseSerilog()

There is an opportunity to completely replace the existing ILoggerFactory version with serilog, and this will stop my ASPNET log. That would be fine if I could just plug in the serilog configuration so that it works like the original ASPNET Core engine. However, Serilog.Settings.Configuration does not seem to copy the configuration to Serilog.

I am also worried that they cannot use the shared resource, what if I use other libraries that use, say, NLog, how do I integrate this into the same logging infrastructure?

Is it possible to configure Serilog in the same way as Microsoft.Extensions.Logging? And if so, how is this achieved?

+4
source share
1 answer

In yours Program.csyou will have this (after loading yours IConfigurationRoot):

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .WriteTo.Console()
    .CreateLogger();

"" , , appsettings.json.

appsettings.json , :

{
    "Serilog": {
        "MinimumLevel": {
            "Default": "Information",
            "Override": {
                "Microsoft": "Warning",
                "System": "Error"
            }
        },
        "WriteTo": [{
            "Name": "Seq",
            "Args": {
                "serverUrl": "http://localhost:5341"
            }
        }],
        "Enrich": ["FromLogContext"],
        "Properties": {
            "Application": "MyApplication"
        }
    }
}

Serilog.Log:

Log.Information("JSON example {@Json}", json);

Microsoft.Extensions.Logging.ILogger:

logger.LogInformation("JSON example {@Json}", json);

.

+2

Source: https://habr.com/ru/post/1688572/


All Articles