serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
456 stars 129 forks source link

Question: Copy Trace Switches from `Logging` Configuration Section? #295

Closed Mike-E-angelo closed 2 years ago

Mike-E-angelo commented 2 years ago

Hello,

I have been configuring Seq and Serilog in my project. Everything was really easy to integrate and configure, and I encountered very little friction. Thank you for all of your efforts out there. 🙏

With my configuration, I do have a question, and it has to do with Serilog.

My appsettings.json looks like the following:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Starbeam": "Debug"
        }
    },
    "Serilog": {
        "LevelSwitches": { "$controlSwitch": "Information" },
        "MinimumLevel": { "ControlledBy": "$controlSwitch" },
        "WriteTo": [
            {
                "Name": "Seq",
                "Args": {
                    "serverUrl": "http://localhost:5341",
                    "apiKey": "...",
                    "controlLevelSwitch": "$controlSwitch"
                }
            }
        ]
    }

}

Notice the Logging section. I have everything working how I would like, but would prefer to have the level switches copied from the Logging section. Is this possible?

Thank you for any assistance you can provide.

skomis-mm commented 2 years ago

Hi @Mike-E-angelo , just move to the Override subsection:

"MinimumLevel": {
    "ControlledBy": "$controlSwitch",
    "Override": {
        "Microsoft": "Warning",
        "Starbeam": "Debug"
    }
}
Mike-E-angelo commented 2 years ago

Thank you for your reply @skomis-mm. I did see there was an override, but that is defined in Serilog.Events.LogEventLevel values, correct? Ideally, I would like to use one set of logging switches and those are the ones configured in the Logging element in my configuration (for the Microsoft.Extensions.Logging API).

Perhaps it will assist to provide a little more information on what I am doing here. To provide further context with my scenario, I am using Microsoft.Extensions.Logging throughout my solution, along with configured outputs: console, debug, etc. Everything is already configured as I like it, and as a last step before deployment, I wanted to add a Seq output as well. I have used both Seq and Serilog in the past and have enjoyed them, especially the enrichers.

So I started by adding the Seq.Extensions.Logging package to my solution, and did have luck setting that up. That actually worked exactly as desired, but I was not able to get all the nifty enrichers configured/sent along with the logging. As I mentioned, I have used both Serilog and Seq in the past and was apparently confused which API provided the enrichers.

As you know, and as I have learned, they are from Serilog, not Seq. So that led me back to adding Serilog to my solution, configuring the ILogger with all the enrichers, and calling AddSerilog in the AddLogging call. Unfortunately, that now leads to the problem I have now of having to manage two sets of logging switches: one for Microsoft.Extensions.Logging and one for Serilog.

If there is some way to configure the Seq.Extensions.Logging to include the enrichers, then I am also interested in learning about that as well, as that accomplishes what I really would like to do with my solution.

Thank you for any continued assistance and/or insight that you can provide.

skomis-mm commented 2 years ago

@Mike-E-angelo , I think I understand. If you want to use MEL filters and use Serilog just as additional logger provider then you need custom registration of SerilogLoggerProvider (instead of using AddSerilog extension method) from Serilog.Extensions.Logging package:

.ConfigureLogging((hostContext, loggingBuilder) =>
{
    // sample of inline initialization
    var logger = new LoggerConfiguration()
        .ReadFrom.Configuration(hostContext.Configuration)
        .CreateLogger();

    var serilogProvider = new SerilogLoggerProvider(logger, dispose: true);

    loggingBuilder.AddProvider(serilogProvider);

})
Mike-E-angelo commented 2 years ago

EXCELLENT @skomis-mm! That was exactly what I was looking for. Thank you so much for pointing me in the right direction. It is greatly appreciated. Happy New Year!