Serilog:记录到不同的文件

Serilog : Log to different files

我正在将所有类型的事件记录到单个 Json 文件中,而不考虑 LogLevel。现在我需要将一些自定义性能计数器记录到单独的 Json 文件中。如何在 Serilog 中完成此操作。我是否应该创建不同的记录器实例并在我要记录性能计数器的地方使用它?想将其与 LibLog

一起使用

您可以通过首先确保性能计数器事件标记有特定的 属性 值(LibLog 中的 OpenMappedContext())或来自特定的 type/namespace.

var log = LogProvider.For<MyApp.Performance.SomeCounter>()
log.Info(...);

配置 Serilog 时,应用过滤器的 sub-logger 可以只将所需的事件发送到第二个文件。

Log.Logger = new LoggerConfiguration()
    .WriteTo.Logger(lc => lc
        .Filter.ByExcluding(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("first.json", new JsonFormatter()))
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("second.json", new JsonFormatter()))
    .CreateLogger();

如果你想有一个独立的日志记录管道,只需再创建一个实例。 这是抄袭改编自https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers:

using (var performanceCounters = new LoggerConfiguration()
        .WriteTo.File(@"myapp\log.txt")
        .CreateLogger())
{
    performanceCounters.Information("Performance is really good today ;-)");

    // Your app runs, then disposal of `performanceCounters` flushes any buffers
}

我们也可以在配置文件中进行设置。下面给出的是 appsettings.json 中的示例,用于根据级别

将日志拆分为滚动文件
{
 
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "(@Level = 'Error' or @Level = 'Fatal' or @Level = 'Warning')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/ex_.log",
                  "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "(@Level = 'Information' or @Level = 'Debug')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/cp_.log",
                  "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                }
              }
            ]
          }
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName"
    ],
    "Properties": {
      "Application": "MultipleLogFilesSample"
    }
  }
}

那么,你只需要修改Program.cs文件中的CreateHostBuilder方法即可从配置文件中读取

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseSerilog((hostingContext, loggerConfig) =>
                loggerConfig.ReadFrom.Configuration(hostingContext.Configuration)
            );

更多详细信息,请参阅 post here

我正在使用以下代码将不同的级别记录到不同的文件。

var conn = configuration.GetSection("ConnectionStrings:SqlConn").Value;

var serilogLogger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.Console()
            .WriteTo.MSSqlServer(conn, new MSSqlServerSinkOptions { TableName = "ErrorLogs", AutoCreateSqlTable = true }, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning)
            .WriteTo.MSSqlServer(conn, new MSSqlServerSinkOptions { TableName = "InfoLogs", AutoCreateSqlTable = true }, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
            .CreateLogger();