Standalone configuration file for serilog

Viewed 210

I'm looking for a way to provide a standalone XML/JSON configuration file for serilog which is dynamicaly loaded from the directory the application itself runs in.

I'm looking for something similiar to what NLog provides. As described in Nlog#Configuration-file:

For a stand-alone *.exe application, files are searched as follows:

  • standard application configuration file (usually applicationname.exe.config)
  • applicationname.exe.nlog in application’s directory
  • NLog.config in application’s directory (Name sensitive; using docker dotnet core)
  • NLog.dll.nlog in a directory where NLog.dll is located (only if NLog isn't installed in the GAC)

Is there any similar behaviour in serilog?

1 Answers

I found the answer myself in this wiki.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("debug.json")
    .Build();

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

Where debug.json is a file in the application directory which can be changed. My sample file looks like

{
  "Serilog": {
    "Using":  [ "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "File", "Args": { "path": "log.log" } }
    ],
    "Properties": {
        "Application": "Sample"
    }
  }
}
Related