Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to detect if Serilog's Log.Logger has already been configured?

I am using Serilog in both a class library and a .NET console application (C#). The logger is currently configured in the console application:


Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.File(
        "verboseLog.txt", 
        restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
    )
    .CreateLogger();

I would like to conditionally configure it in the library if the caller has not configured it yet. (This library may be used with other applications in the future by other programmers.) Serilog’s wiki on Github doesn’t mention any default value for Log.Logger, and I haven’t found an answer anywhere else. Is there a way to detect if the static Log.Logger has already been configured in Serilog?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Configuring the logging system in a library is not a good idea. Logging configuration is something you want to do at the application level. I’d wager that there’s a good reason you can’t know if the logger is configured or not: because you don’t need to know! 😉

With that said, here’s a (dirty) solution to figure out if the logger is configured or not:

bool isLoggerConfigured = Log.Logger.GetType().Name != "SilentLogger";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading