I'm working on a legacy .NET Framework console application. (No ASP.NET or .NET Core logging frameworks are involved).
The VS solution contains the console application project, and also a separate class library project which the console application depends on.
I have the following Serilog-related NuGet packages installed in the two C# projects:
Console application assembly
- Serilog 2.10.0
- Serilog.Enrichers.Demystifier 1.0.1
- Serilog.Sinks.Console 4.0.0
- Serilog.Sinks.File 5.0.0
- Various dependencies of the above
Library assembly
- Serilog 2.10.0
- Direct dependencies of Serilog
I am configuring the global static logger at the top of my console application like so:
static void Main(String[] args)
{
// Configure Serilog static logger
Log.Logger = new LoggerConfiguration()
.Enrich.WithDemystifiedStackTraces()
.WriteTo.Console(LogEventLevel.Information)
.WriteTo.File("app-.log",
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
restrictedToMinimumLevel: LogEventLevel.Debug
)
.CreateLogger();
Calls to Log.Information()
, Log.Warning()
, Log.Error()
etc work fine as long as I'm calling them from the context of the console application assembly.
But when the console app calls into methods that are defined in the library assembly, calling Log.Information()
and friends has no visible effect. The expected log messages don't show up in the Console or File sinks.
Once the library method returns, back to the Console assembly, the Log
static methods work once again.
What's going on here? Why does the global static Log.Logger
seem to have different configuration or behaviour when in the scope of the library code? The documentation gave me the impression that I only needed to set Log.Logger
once, and then I would be able to use the static methods like Log.Information()
from anywhere. Am I supposed to be passing my configured logger explicitly across the assembly boundary, or somehow disambiguating between multiple global Log.Logger
instances?