12

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".

mosakashaka
  • 535
  • 1
  • 6
  • 19
  • If I'm not wrong it should be possible to define an absolute path for the `WriteTo` argument!? So you could define an environmental variable for your application using `Environment.SetEnvironmentVariable("MY_APP_DIR", MyAppDir)` and define "%MY_APP_DIR%\\Logs\\serilog-configuration-sample.txt" as your output path... – Robert Apr 02 '19 at 12:53
  • @Robert thank you, that works. I'm think there should be a way to configure it directly by the config file, but it seems that it's currently the only approach to use environment variable... – mosakashaka Apr 03 '19 at 00:39

2 Answers2

11

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

ElasticCode
  • 7,311
  • 2
  • 34
  • 45
2

You can add a "RollingFile" wich can write to a local path file. In this example I'm writing in a File inside of the root of my project as it shows bellow.

{
    "Name": "RollingFile",
    "Args": {
      "pathFormat": ".\\Logs\\logs.txt",
      "fileSizeLimitBytes": 1048576
    }
  },

Also the full json on appsettings.json end up like this (in case you need a full example)

...
"Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "System": "Debug",
        "Microsoft": "Debug"
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsightsEvents",
        "Args": {
          "instrumentationKey": "xxxxxxxxxx"
        }
      },
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": ".\\Logs\\logs.txt",
          "fileSizeLimitBytes": 1048576
        }
      },
      { "Name": "Console" },
      {
        "Name": "EventLog",
        "Args": {
          "source": "API NAME",
          "logName": "CustomLog",
          "restrictedToMinimumLevel": "Warning"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
      "Application": "API NAME"
    }
  }
...
Simon Restrepo
  • 313
  • 1
  • 2
  • 15
  • add a dot sign before the path seems to have no effect.... the log is still writing to IIS directory when I debug it in the visual studio... – mosakashaka Apr 03 '19 at 00:36