Search code examples
asp.net-corewindows-servicesserilog

Unable to start multiple Serilog sinks from Asp.net core website hosted in windows service


My service runs fine and it is writing to event log serilog sink just fine too. My problem is that serilog refuses to write to any other sink provided in appsettings.json My Appsettings.json snippet

 "Serilog": {
    "Using": [ "Serilog.Sinks.EventLog" ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "System": "Information",
        "Microsoft": "Information"
      }
    },
    "WriteTo": [

      {
        "Name": "EventLog",
        "Args": {
          "source": "FMS",
          "manageEventSource": true
        }
      },          
      {
        "Name": "RollingFile",
        "Args": {

          "pathFormat": "D:\\publish\\log-{Date}.txt",
          "rollingInterval": "Day",
          "fileSizeLimitBytes": 1024,
          "retainedFileCountLimit": 60,
          "shared": true,
           "buffered": true
        }
      }
    ],
    "Enrich": [ "WithProcessId", "WithProcessName", "WithExceptionDetails" ]
  }

I am setting up my logger in my CustomWebHostService as

public IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Data.GetDirectoryPath())//gets directory
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        public CustomWebHostService(IWebHost host) : base(host)
        {
            Log.Logger = new LoggerConfiguration()
              .ReadFrom.Configuration(Configuration)
              .CreateLogger();
        }

I have tried creating a console sink and a plain old file sink too, nothing works except the eventlog sink for me. I have updated to the latest stable version of all the required nuget packages.

edit: I've tried and the service CAN write to the location.


Solution

  • The problem was this line:

    "Using": [ "Serilog.Sinks.EventLog" ],
    

    You need to specify all the sinks if you're using this syntax. It can be completely removed and the sinks will work (or you can specify it so you can easily turn sinks off using one line).