如何在 appsettings.json 中使用多个单独的记录器 Serilog 文件

Posted

技术标签:

【中文标题】如何在 appsettings.json 中使用多个单独的记录器 Serilog 文件【英文标题】:How to use multiple separate logger Serilog file in appsettings.json 【发布时间】:2021-09-08 12:56:34 【问题描述】:

我正在尝试使用 Serilog.Sinks.File 配置 Serilog 两个不同的文件来存储 MVC 项目和另一个 Serilog.Sinks.MSSqlServer 我的 SQL 服务器表。如何创建两个不同的地方存储错误文件?

这是我的 appsettings.json 文件代码

"FileLog": 
    "Using": [ "Serilog.Sinks.File" ],
    "WriteTo": [
      
        "Name": "File",
        "Args": 
          "path": "Logs/web-log-.log",
          "rollingInterval": "Day"
        
      
    ]
  ,
  
    "DatabaseLog": 
    "Using": [ "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": "Warning",
    "WriteTo": [
      
        "Name": "MSSqlServer",
        "Args": 
          "connectionString": "Server = DESKTOP-P9UP8PR\\SQLEXPRESS; Database = MVC_Project; User Id = MizanurRahman; Password = 682672;",
          "tableName": "Logs",
          "autoCreateSqlTable": true
        
      
    ]
  ,

【问题讨论】:

【参考方案1】:

我不使用数据库日志记录,但是对于多个文件日志记录 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/MyFirstLogFile.log",
                  "outputTemplate": "Timestamp:o [Level:u3] (SourceContext) MessageNewLineException",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                
              
            ]
          
        
      ,
      
        "Name": "Logger",
        "Args": 
          "configureLogger": 
            "Filter": [
              
                "Name": "ByIncludingOnly",
                "Args": 
                  "expression": "(@Level = 'Information' or @Level = 'Debug')"
                
              
            ],
            "WriteTo": [
              
                "Name": "File",
                "Args": 
                  "path": "Logs/MySecondLogFile.log",
                  "outputTemplate": "Timestamp:o [Level:u3] (SourceContext) MessageNewLineException",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                
              
            ]
          
        
      
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName"
    ],
    "Properties": 
      "Application": "MyApplication"
    
  

【讨论】:

以上是关于如何在 appsettings.json 中使用多个单独的记录器 Serilog 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 _layout.chtml 中读取 appsettings.json

如何在 appSettings.json 中硬编码和读取字符串数组?

如何在使用 .NET Core 的控制台应用程序中从 appsettings.json 获取值?

如何在 .NET Core MVC 项目中转换 appsettings.json?

您如何以与其前身 webforms 相同的风格从 appsettings.json 中读取信息?

如何在运行时在 Blazor WebAssembly 客户端应用程序中使用 Azure 应用设置作为 appsettings.json 配置?