23

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

Best regards Gope

Gope
  • 1,522
  • 1
  • 11
  • 18

7 Answers7

36

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!

Michael
  • 1,443
  • 14
  • 11
12

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.

Jafin
  • 3,135
  • 35
  • 48
9
 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

  • How can I specify app domain path in appsettings.json? – kudlatiger Jun 20 '18 at 06:26
  • dear @kudlatiger, here is the example./r/e `var myDomainPath = Configuration["domainPath"];` `loggerFactory.AddFile(Path.Combine(myDomainPath, "ApiLog-{Date}.txt"));` _appsettings.json_ `"domainPath": "myDomainPath",` – Grigor Yeghiazaryan Oct 30 '18 at 16:12
4

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.

Nicholas Blumhardt
  • 25,642
  • 3
  • 71
  • 87
  • 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! – Gope Oct 29 '14 at 09:18
  • 4
    I found that when my service logs here: `` that it ends up in `C:\Windows\SysWOW64`, which is apparently the current directory when the service first starts. FYI. – Glenn Doten Jun 10 '15 at 13:54
1

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();
liuhongbo
  • 1,753
  • 3
  • 20
  • 29
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"
        }
    }
}
David Buck
  • 3,439
  • 29
  • 24
  • 31
Gina Marano
  • 1,459
  • 3
  • 20
  • 34
0

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();