I have a .net5.0 console app using Serilog via the...
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
All working fine in dev. When I publish using "Produce single file", the app works, but the logging doesn't. I am logging to file and console... I see neither.
If I publish without using the single file, the logging works OK.
At first I thought it was an issue with reading the appsettings.json, but some simple Console.Writeline confirms the appsettings file is being read OK (at least from the app itself, can I confirm any way that Serilog is reading it OK?)
There are a few issues with NET5 single file applications with Serilog due to the way NET5 bundles/extracts compared to the way that 3.x did.
First, per the documentation for the Serilog.Settings.Configuration
library (bolding and code blocks mine):
Currently, auto-discovery of configuration assemblies is not supported in bundled mode. Use
Using
section for workaround.
This means your appsettings.json file needs to look like the following (assuming you are using the Console and File sinks):
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"WriteTo": [
{ "Name": "Console" },
{ "Name": "File", "Args": { "path": "Logs/log.txt" } }
],
// remaining config
}
}
Now depending on which version of Serilog.Settings.Configuration
you are using, you may need this workaround as well:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config, "Serilog", ConfigurationAssemblySource.AlwaysScanDllFiles)
.CreateLogger();
Instead, you can also revert to netcore3's bundling behavior by modifying your project file as follows:
<PropertyGroup>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>