Serilog in Windows-Service not writing to logfile

十年热恋 提交于 2019-12-04 22:50:27

I had a very similar issue. In my case the problem was with relative paths. I just had to specify absolute path. Now it works like a charm.

WriteTo.RollingFile(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\log-{Date}.log")

Hope it helps!

We had the same issue, this is what we found:

  • Serilog configured for rolling logfiles and writing to Seq
  • Ddrable logs was enabled.

Symptoms - No log files were being created - No logs written to Seq.

As per @gdoten's comment, our log files were being written to \windows\syswow64 (service was running as localservice).
We believe the permissions on these files may not of allowed the durable spool file to be read causing no logs to be written to Seq.

Fix was to hard code the path of the rollinglogfile and buffer.

 loggerFactory.AddFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log-{Date}.txt"));

Try this one, it worked in my ASP.NET core 2 application runs as windows service

It's most likely that the account under which the services is running lacks permission to write to the log file location. Try changing the log file location to the system's temp folder to see if this is the case.

If this still fails, using Serilog's SelfLog to get exception information is your best bet.

I am running as a windows service and here is my json file.

{
    "Serilog": {
        "MinimumLevel": "Debug",
        "WriteTo": [
          {
            "Name": "RollingFile",
            "Args": {
              "pathFormat": "C:\\Program Files\\mycomp\\myapp\\logs\\log-{Date}.txt"
          }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithEnvironmentUserName", "WithProcessId" ],
    "Properties": {
      "Application": "MyApp",
      "Environment": "Development"
}

} }

-Gina

I had a similar issue, it ends up because the following code,

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

the code specify the appsettings.json file, and in that file it has the serilog's configuration setttings, but when run in service, the current folder is not point to the executable file folder. so it can not find the file appsettings.json, and then of course serilog wont work as expected.

just need to change the code as following will make it work

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!