简体   繁体   English

Syslog的Serilog asp.net核心应用设置配置

[英]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. 我正在尝试使用ASP.NET Core Web应用程序中的appsettings.json配置文件配置Serilog。 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) 我设法使RollingFile config部分起作用,但是我也试图将日志信息发送到另一个在线日志工具(例如logentries或papertrail)

I have the following nuget packages installed: 我安装了以下nuget软件包:

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: 然后,将以下配置放入我的appsettings.json文件中:

"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: 我设置记录器的C#代码是:

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? 我在Syslog配置块中缺少什么吗?

When configuring Serilog using the IConfiguration approach, the Name value is not necessarily the name of the sink itself. 使用IConfiguration方法配置Serilog时,“ Name值不一定是接收器本身的名称。 For Serilog.Sinks.RollingFile , it is simply RollingFile , but for Serilog.Sinks.Syslog , you actually have three options: 对于Serilog.Sinks.RollingFile ,那简直就是 RollingFile ,但对于Serilog.Sinks.Syslog ,你实际上有三个选项:

  • 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: 例如对于Serilog.Sinks.Syslog ,示例为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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