6

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?)

dllhell
  • 1,987
  • 3
  • 34
  • 51
dizidave
  • 63
  • 4

1 Answers1

9

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>
pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63
  • 1
    As a friendly reminder, don't forget to check github when you have issues with open source libraries. There were at least two issues in the project with titles containing text directly from your title/question and usually the project maintainers are the best people to ask! Though... I just happened to have had this exact issue during my migration so already knew where to look – pinkfloydx33 Feb 19 '21 at 12:38
  • 1
    The "Using" in the appsettings did the trick pinkfloydx33. Thanks for the help, and i take on board your comment about the github resources. Many thanks. – dizidave Feb 22 '21 at 14:55