取消

serilog配置文件示例

serilog配置文件示例


json配置示例

需要安装Serilog.Settings.ConfigurationSerilog.Expressions

1
2
3
4
5
6
7
8
9
10
11
12
 Serilog.Log.Logger = new LoggerConfiguration()
     .ReadFrom.Configuration(BuildConfiguration())
     .CreateLogger();
public static IConfiguration BuildConfiguration()
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
        .AddJsonFile("appsettings.json");

    var config = builder.Build();
    return config;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
  "Serilog": {
    "Using": [ "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "System": "Information",
        "Microsoft": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "DiagnosticTrace" },
      {
        "Name": "Conditional", //必须使用Conditional,否则expression不起效
        "Args": {
          "expression": "@l in ['Error', 'Fatal']",
          "configureSink": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/Error-.log",
                "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
                "rollingInterval": 3, //1 2 3 4小时
                "Shared": true,
                "RollOnFileSizeLimit": true,
                "FileSizeLimitBytes": 1048576,
                "RetainedFileCountLimit": 1000
              }
            }
          ]
        }
      },
      {
        "Name": "Conditional",
        "Args": {
          "expression": "@l = 'Information'",
          "configureSink": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/Info-.log",
                "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
                "rollingInterval": 3,
                "Shared": true,
                "RollOnFileSizeLimit": true,
                "FileSizeLimitBytes": 1048576,
                "RetainedFileCountLimit": 1000
              }
            }
          ]
        }
      },
      {
        "Name": "Conditional",
        "Args": {
          "expression": "@l in ['Verbose', 'Debug']",
          "configureSink": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/Debug-.log",
                "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
                "rollingInterval": 3,
                "Shared": true,
                "RollOnFileSizeLimit": true,
                "FileSizeLimitBytes": 1048576,
                "RetainedFileCountLimit": 1000
              }
            }
          ]
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
    //"Properties": {
    //  "Application": "Sample"
    //}
  }
}

参考资料 serilog-settings-configuration

本文会经常更新,请阅读原文: https://dashenxian.github.io/post/serilog%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%A4%BA%E4%BE%8B ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 小神仙 (包含链接: https://dashenxian.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (125880321@qq.com)

登录 GitHub 账号进行评论