简体   繁体   中英

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 (eg 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?

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. eg for Serilog.Sinks.Syslog , the examples are:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM