Serilog : Log to different files

后端 未结 3 2347
广开言路
广开言路 2021-02-18 17:38

I am logging events of all types to single Json file irrespective of LogLevel. Now I have a requirement to log some custom performance counters to a seperate Json file. How can

3条回答
  •  没有蜡笔的小新
    2021-02-18 18:13

    We can set it up in the configuration files too. Given below is a sample in the appsettings.json to split the log the rolling files based on levels

    {
     
      "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"
        }
      }
    }
    

    Then, you will only need to modify the CreateHostBuilder method in Program.cs file to read it from the configuration file

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

    For more details, refer the post here

提交回复
热议问题