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?
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 solution to figure out if the logger is configured or not:
bool isLoggerConfigured = Log.Logger != Logger.None;
It should be quite safe to use since a unit test ensures that Logger.None
is a singleton.