Serilog in Windows-Service not writing to logfile
Asked Answered
C

8

37

I am using Serilog within an TopShelf Service, logging to the console and a rolling file. When running the service in a console my messages are written to the logfile, but when I install the service and run it no logging occurs. Is there anything special I need to configure? The file is written to the binaries folder under ".\logs\log-{date}.txt".

Cymar answered 26/10, 2014 at 10:57 Comment(1)
I have just the same issue, did you have a chance to find solution?Aitchbone
A
54

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

WriteTo.RollingFile(
  AppDomain.CurrentDomain.BaseDirectory + "\\logs\\log-{Date}.log"
)
Aitchbone answered 5/5, 2015 at 10:11 Comment(2)
In <appSettings> configuration, you can also use environment variables like %TEMP% in the path - putting logs under %APPDATA% or similar can work well.Liaoning
AppDomain.CurrentDomain.BaseDirectory applies to NET 5-6-7 ?Hubby
E
15

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

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

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/workaround was to hard code the path of the rollinglogfile and buffer.

Edaphic answered 24/3, 2016 at 5:16 Comment(1)
mine was actually in Windows\System32Braun
A
8
 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

Arrowy answered 1/2, 2018 at 21:32 Comment(2)
How can I specify app domain path in appsettings.json?Whiffen
dear @kudlatiger, here is the example./r/e var myDomainPath = Configuration["domainPath"]; loggerFactory.AddFile(Path.Combine(myDomainPath, "ApiLog-{Date}.txt")); appsettings.json "domainPath": "myDomainPath",Arrowy
L
6

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.

Liaoning answered 27/10, 2014 at 21:47 Comment(2)
Hello Nicholas, thanks for your reply. The service is running as LocalSytem and has full rights for the directory (excl. special rights). I am currently using Log4Net just because it works in the console AND within the service. As soon as I change the Logging class to SeriLog it does not write to or even create the logfile when running as a service. Still in console-mode it works... o_o I will try checking SelfLog, thanks for that tip!Cymar
I found that when my service logs here: <add key="serilog:write-to:RollingFile.pathFormat" value="ServerServiceApp-{Date}.log" /> that it ends up in C:\Windows\SysWOW64, which is apparently the current directory when the service first starts. FYI.Passepartout
R
2

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 won't 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();
Rickyrico answered 11/4, 2019 at 21:56 Comment(0)
L
0

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

Literalminded answered 17/1, 2019 at 0:20 Comment(1)
I had this issue as well when using a Windows service and configuration settings. Turns out the issue was not permission related. Rather (using a clue from above thread to enable selflog), it was revealed that the run time couldn't find the assemblies for the various sinks (Console, File). I had to add a new section in the Serilog configuration : "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Environment" ]. See github.com/serilog/serilog-settings-configuration. I don't know why Windows Service makes a difference in finding the assemblies.Pettit
I
0

I wanted to read the appsettings.json and wright log files back to the application's directory, instead of the system32 directory the service was running in. Starting with this line of code fixed both issues for me.

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

https://mcmap.net/q/269291/-windows-service-get-current-directory

Inhesion answered 9/5, 2022 at 14:24 Comment(0)
C
-1

You can also use something like this (When you want to create a Windows Service and Serilog):

var builder = new ConfigurationBuilder()
           .AddJsonFile($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\appsettings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables();
Collado answered 18/2, 2020 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.