serilog / serilog-settings-configuration

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

Console application fails after upgrading to Serilog 7 #394

Closed SoftCircuits closed 1 year ago

SoftCircuits commented 1 year ago

My console application has been working fine. Recently, I made some changes and redeployed it, and now it's throwing an exception.

Unhandled exception. System.InvalidOperationException: No Serilog:Using configuration section is defined and no Serilog assemblies were found. This is most likely because the application is published as a single-file.

enter image description here

None of my changes were related to Serilog or logging. However, I did refresh all the NuGet packages with updates, including updating Serilog.AspNetCore from 6.1.0 to 7.0.0.

While it's true I have the Produce single file option checked, this options was working and I'd prefer to keep using it.

Does anyone know what might have changed in Serilog version 7 that could cause this?

Here's my configuration:

// Configure Serilog
string logFormat = "[{Timestamp:yyyy-MM-dd hh:mm:ss tt}][{Level:u3}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(appServices.Configuration)
    .WriteTo.Console(LogEventLevel.Verbose, logFormat)
    .WriteTo.File(LogFile, LogEventLevel.Verbose, logFormat)
    .WriteTo.List(LogEvents)
    .CreateLogger();

I'm using .NET 7.0. And here's my configuration section.

"Serilog": {
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  }
}
0xced commented 1 year ago

Thank you for the bug report. This issue has already been brought to our attention in #389. It is already fixed by #391 which is merged in the dev branch but a new NuGet package has not yet been released.

In the meantime, the recommended workaround is to specify one assembly through the ConfigurationReaderOptions object.

var options = new ConfigurationReaderOptions(typeof(ConsoleLoggerConfigurationExtensions).Assembly);
string logFormat = "[{Timestamp:yyyy-MM-dd hh:mm:ss tt}][{Level:u3}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(appServices.Configuration, options)
    .WriteTo.Console(LogEventLevel.Verbose, logFormat)
    .WriteTo.File(LogFile, LogEventLevel.Verbose, logFormat)
    .WriteTo.List(LogEvents)
    .CreateLogger();
SoftCircuits commented 1 year ago

@0xced Thanks for the timely response. I will review your workaround and keep an eye out for the next update. I found my project also runs if I just don't produce a single file.

nblumhardt commented 1 year ago

@0xced I've queued up #395 - think this is ready to merge/release?

0xced commented 1 year ago

Yes, releasing 7.0.1 with these two bug fixes is a good idea in my opinion.

0xced commented 1 year ago

Serilog.Settings.Configuration 7.0.1, which fixes this issue, is now released on NuGet, enjoy! 🚀

SoftCircuits commented 1 year ago

@0xced Sorry, but I had another question about this.

Actually, I had not installed Serilog.Settings.Configuration. I thought you were updating the main component. Would installing this package still fix this issue for me?

0xced commented 1 year ago

Serilog.Settings.Configuration is a dependency of the Serilog.AspNetCore package, as you can see on its NuGet page: Serilog.Settings.Configuration (>= 7.0.0) It means that even though you have not explicitly installed this package, it is already (implicitly) installed as a dependency.

So yes, installing version 7.0.1 explicitly of Serilog.Settings.Configuration will solve your issue.

SoftCircuits commented 1 year ago

@0xced Cool. Thanks again. I guess the NuGet manager in Visual Studio isn't smart enough to see I have that dependency.

0xced commented 1 year ago

I haven't used Visual Studio in years so I can't tell. But JetBrains Rider helps understanding implicitly installed packages, see screenshot. 😉

Serilog Settings Configuration implicit dependency