【问题标题】:How to use multiple separate logger Serilog file in appsettings.json如何在 appsettings.json 中使用多个单独的记录器 Serilog 文件
【发布时间】: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
        }
      }
    ]
  },

【问题讨论】:

    标签: c# sql asp.net-mvc serilog-sinks-file


    【解决方案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}) {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/MySecondLogFile.log",
                      "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
                      "rollingInterval": "Day",
                      "retainedFileCountLimit": 7
                    }
                  }
                ]
              }
            }
          }
        ],
        "Enrich": [
          "FromLogContext",
          "WithMachineName"
        ],
        "Properties": {
          "Application": "MyApplication"
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多