Search code examples
c#.net-coreserilog

How to config `Serilog` to write to the application directory with the cofig file?


I'm using Serilog on a .net core. I want to config the log path to the application directory.

I see there's an extension https://github.com/serilog/serilog-settings-configuration that enable Serilog to read from Configuration. In the example , the path is configured as "%TEMP%\\Logs\\serilog-configuration-sample.txt". How can I set it to the working directory?

I've searched on so, and know that it can be done by code, but it seems there's no one asking how to do this by the config file, i.e. appsettings.json.

Current configuration:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.File"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": { "path": "Logs\\serilog-configuration-sample.txt" }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Destructure": [
    ],
    "Properties": {
    }
  },
  "AllowedHosts": "*"
}

I want the log path to be set to the working directory. But currently it's in "C:\Program Files\IIS Express".


Solution

  • Configuring path like Logs/log.txt will write log files under logs folder in working directory

    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs/log.txt"
        }
      }
    

    Also you can check this answer for another option