Search code examples
c#asp.net-coreserilog

serilog not writing logs into local app data folder


I am trying to write log into "LocalApplicationData" using Serilog and using below configuration, but it's not working. If I try to write in some hard code path like "path": "C:/Temp/Logs/log.txt" then this is working. How to write into local app data folder?

"WriteTo": [
  {
    "Name": "File",
    "Args": {
      "path": "${specialfolder:folder=LocalApplicationData}/Logs/log.txt",
      "fileSizeLimitBytes": "5242880",
      "rollingInterval": "Day",
      "retainedFileCountLimit": "15",

Solution

  • This format is used in NLog. The equivalent is to use %LOCALAPPDATA% instead:

    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "%LOCALAPPDATA%/Logs/log.txt",
          "fileSizeLimitBytes": "5242880",
          "rollingInterval": "Day",
          "retainedFileCountLimit": "15",
    

    Please note that this will only work in appSettings.json. To do the equivalent in C# you can use Environment.GetEnvironmentVariable("LocalAppData")