ASP.NET Core 6 Web API:serilog subloggers 配置相关问题

问题描述 投票:0回答:1

正如标题所说,我正在努力解决 Serilog 的 subloggers 配置(来自

appsettings.json
)。

我尝试拥有两个子记录器:第一个子记录器仅记录我从服务发送的信息级别日志,第二个子记录器必须将所有内容(可能是 ASP.NET Core 默认日志记录)记录到控制台。

现在我将发布两个我尝试过的

appsettings.json
片段并解释他们在做什么

使用此方法,两个子记录器仅记录从服务发送的日志(可能是由于全局 Microsoft 错误命名空间覆盖):

"Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "Error"
        }
    },
    "WriteTo": [
        {
            "Name": "MSSqlServer",
            "Args": {
                "connectionString": "Server=localhost\\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
                "tableName": "ActivityLogs",
                "autoCreateSqlTable": false,
                "autoCreateSqlDatabase": false,
                "columnOptionsSection": {
                    "customColumns": [
                        {
                            "ColumnName": "UserId",
                            "DataType": "nvarchar"
                        },
                        {
                            "ColumnName": "ItemId",
                            "DataType": "int"
                        }
                    ]
                }
            }
        },
        {
            "Name": "Console",
            "Args": {
                "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
            },
            "MinimumLevel": "Verbose"
        }
    ]
}

这会导致控制台根据需要记录所有内容,但数据库子记录器不记录任何内容:

"Serilog": {
     "MinimumLevel": {
     "Default": "Information"
     },
     "WriteTo": [
      {
       "Name": "MSSqlServer",
       "Args": {
         "connectionString": "Server=localhost01\\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
        "tableName": "ActivityLogs",
        "autoCreateSqlTable": false,
        "autoCreateSqlDatabase": false,
        "columnOptionsSection": {
          "customColumns": [
            {
              "ColumnName": "UserId",
              "DataType": "nvarchar"
            },
            {
              "ColumnName": "ItemId",
              "DataType": "int"
            }
          ]
        }
      }
    },
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
      },
      "MinimumLevel": "Verbose"
    }
  ]
}

我明白问题出在哪里,但经过多次尝试仍然无法解决

c# asp.net-core-webapi serilog
1个回答
0
投票

好吧,看来我已经正常工作了

这是一个有效的

appsettings.json
配置:

"Serilog": {
  "MinimumLevel": {
    "Default": "Verbose"
  },
  "Using": [ "Serilog.Expressions" ],
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "configureLogger": {
          "WriteTo": [
            {
              "Name": "MSSqlServer",
              "Args": {
                "connectionString": "Server=localhost\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
                "tableName": "ActivityLogs",
                "autoCreateSqlTable": false,
                "autoCreateSqlDatabase": false,
                "columnOptionsSection": {
                  "customColumns": [
                    {
                      "ColumnName": "UserId",
                      "DataType": "nvarchar"
                    },
                    {
                      "ColumnName": "ItemId",
                      "DataType": "int"
                    }
                  ]
                }
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByExcluding",
              "Args": {
                "expression": "StartsWith(SourceContext, 'Microsoft.')"
              }
            }
          ]
        }
      }
    }
  ]
}

解决方案是使用

Microsoft.
包过滤掉
Serilog.Expressions
命名空间错误,并确保它将在 Serilog 配置的
Using
部分中使用。

© www.soinside.com 2019 - 2024. All rights reserved.