Emma Middlebrook
Emma Middlebrook

Reputation: 876

Serilog ASP.NET Core appsettings config for syslog

I am trying to configure Serilog using the appsettings.json config file in an ASP.NET Core web application. I have managed to get the RollingFile config section to work, but I am also trying to send log information to another online log tool (e.g. logentries or papertrail)

I have the following Nuget packages installed:

Serilog.AspNetCore
Serilog.Settings.Configuration
Serilog.Sinks.Async
Serilog.Sinks.RollingFile
Serilog.Sinks.SyslogMessages

I have then put the following configuration in my appsettings.json file:

"Serilog": {
  "Using": [ "Serilog.Sinks.Async", "Serilog.Sinks.Syslog", "Serilog.Sinks.RollingFile" ],
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Warning",
      "System": "Error"
    }
  },
  "WriteTo": [
    {
      "Name": "Async",
      "Args": {
        "configure": [
          {
            "Name": "RollingFile",
            "Args": {
              "outputTemplate": "[{Timestamp:MMM dd HH:mm:ss}] {Level:u3} {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
              "pathFormat": "C:\\LogFiles\\Application\\{Date}.log",
              "fileSizeLimitBytes": 5000000,
              "retainedFileCountLimit": null
            }
          },
          {
            "Name": "Syslog",
            "Args": {
              "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
              "host": "logs.papertrailapp.com",
              "port": 12345,
              "format": "RFC5424",
              "secureProtocols": "SecureProtocols.None",
              "appName": "Application",
              "facility": "Local7"
            }
          }
        ]
      }
    }
  ]
}

My C# code to set the logger is:

public static void Main(string[] args)
{
    var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{currentEnv}.json", true)
        .AddEnvironmentVariables()
        .Build();

    Log.Logger =
        new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();

    try
    {
        Log.Information("Starting web host");
        CreateWebHostBuilder(args).Build().Run();
    }
    catch (Exception exception)
    {
        Log.Fatal(exception, "Host terminated unexpectedly");
        throw;
    }
    finally
    {
        Log.CloseAndFlush();
    }
}

Am I missing something from the Syslog config block?

Upvotes: 3

Views: 3734

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93153

When configuring Serilog using the IConfiguration approach, the Name value is not necessarily the name of the sink itself. For Serilog.Sinks.RollingFile, it is simply RollingFile, but for Serilog.Sinks.Syslog, you actually have three options:

  • UdpSyslog
  • TcpSyslog
  • LocalSyslog

I find the best way to discover these options is to look at the docs for the code-based configuration. e.g. for Serilog.Sinks.Syslog, the examples are:

.WriteTo.UdpSyslog
.WriteTo.TcpSyslog
.WriteTo.LocalSyslog

Upvotes: 2

Related Questions