Serilog in ASP.NET Core Windows Service cannot write file as Local System : <p>I am running an ASP.NET Core web server as a Windows service and am using Serilog to log to a file in %PROGRAMDATA%. When I run the service as Local System, nothing is logged.</p>

<p>I am using .Net Core 2.2 on Windows 10. I am testing by triggering an error in my service that writes a log event at the error level. I've tried running the service as my own administrative account and the logging works fine; the issue only occurs when running as Local System.</p>

<p>I have other Windows Services using .Net Framework that run as Local System and have no problem logging to %PROGRAMDATA% with Serilog, but this is the first time I have tried it on .Net Core. I can manually create and write to the log file within the service with Directory.CreateDirectory and File.AppendText and that works while running as Local System, but the Serilog logging does not.</p>

<p>Here is my Program.Main:</p>

<pre class="lang-cs prettyprint-override"><code>public static async Task Main(string[] args)
{
    var isService = !(Debugger.IsAttached || args.Contains("--console"));       
    if (isService)
    {
        var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        var pathToContentRoot = Path.GetDirectoryName(pathToExe);
        Directory.SetCurrentDirectory(pathToContentRoot);
    }

    var host = WebHost.CreateDefaultBuilder(args.Where(arg =&gt; arg != "--console").ToArray())
        .UseStartup&lt;Startup&gt;()
        .UseSerilog((hostingContext, loggerConfiguration) =&gt; loggerConfiguration
            .ReadFrom.Configuration(hostingContext.Configuration)
            .Enrich.FromLogContext())
        .Build();

    // additional async initialization omitted

    if (isService)
    {
        host.RunAsService();
    }
    else
    {
        host.Run();
    }
}
</code></pre>

<p>And here is the Serilog section of my appsettings.json:</p>

<pre><code>"Serilog": {
  "MinimumLevel": {
    "Default": "Verbose",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "File",
      "Args": {
        "path": "%PROGRAMDATA%/foo/bar baz/logs/qux.log",
        "fileSizeLimitBytes": 1048576,
        "rollOnFileSizeLimit": "true",
        "retainedFileCountLimit": 99,
        "flushToDiskInterval": "00:00:01",
        "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}"
      }
    },
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}"
      }
    }
  ]
}
</code></pre>

<p>I expected logging to be written to the file in %PROGRAMDATA% when the service is running as Local System, but nothing happens. Logging is written without issue when the service is run as any other administrative account.</p>
