In a controller method, the generic logger doesn't seem to have the defined enrichers.
Here is the controller:
public class TestController : Controller { ILogger _logger; public TestController(ILogger<TestController> logger) { _logger = logger; } public IActionResult action() { try { throw new NullReferenceException(); } catch(Exception e) { Serilog.Log.Error(ex, "action KO"); _logger.LogError("action KO", ex); } } } The appsettings.json:
{ "Serilog": { "MinimumLevel": { "Default": "Debug", }, "WriteTo": [ { "Name": "Console" }, { "Name": "File", "Args": { "path": "Log/api.log", "outputTemplate": "{Timestamp} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ], "Enrich": [ "FromLogContext", "WithExceptionDetails" ] } } Host building:
IHost host = Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.UseUrls($"http://*:12345"); }) .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration .ReadFrom.Configuration(hostingContext.Configuration) ) .Build() ; Output in file / console:
02/18/2021 12:24:57 +01:00 [ERR] () action KO System.NullReferenceException: Object reference not set to an instance of an object. at App.TestController`1.action() 02/18/2021 12:24:57 +01:00 [ERR] (App.TestController) action KO So when I try to use a generic logger, the exception is omitted. Wheras the static logger writes it.
Am I missing something like a provider for controllers logger or is it meant to be done by UseSerilog?
EDIT
Tried
UseSerilogwithwriteToProviders: true=> no effectTried
AddSerilogas a logging builder => no effectservices.AddLogging(loggingBuilder => loggingBuilder.AddSerilog( new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger(), true));Tried AddSerilogServices => no effect
public static IServiceCollection AddSerilogServices( this IServiceCollection services, LoggerConfiguration configuration) { Log.Logger = configuration.CreateLogger(); AppDomain.CurrentDomain.ProcessExit += (s, e) => Log.CloseAndFlush(); return services.AddSingleton(Log.Logger); }