在我的subloggers应用程序中,我设置了一个Serilog,用于将不同类型的事件(信息、警告等)记录到带有子记录器的Server数据库中的不同表中,并且所有事件都进入一个文件。
文件日志记录工作正常,但Server日志记录工作不正常。数据库中的专用表正在自动创建,但没有任何内容被写入其中。我在连接到数据库时使用sa登录,所以这与角色和权限无关。
我目前的配置如下:
"WriteTo": [
{
"Name": "Debug",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:l} {NewLine}"
}
},
{
"Name": "File",
"Args": {
"Path": "D:/file.txt",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:l} {NewLine}"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "(@Level = 'Error' or @Level = 'Fatal')"
}
}
],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=.\\SQLEXPRESS;User Id=sa;Password=*****; Initial Catalog=LogsDb",
"tableName": "NmsRecipesLogs_Errors",
"autoCreateSqlTable": true,
"batchPostingLimit": 1,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Properties" ]
}
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "(@Level = 'Information' or @Level = 'Warning')"
}
}
],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=.\\SQLEXPRESS;User Id=sa;Password=****;Initial Catalog=LogsDb",
"tableName": "NmsRecipesLogs_Info",
"autoCreateSqlTable": true,
"batchPostingLimit": 1,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Properties" ]
}
}
}
]
}
}
}
]我知道MSSQLServer接收器不会立即将日志写入数据库,所以我将batchPostingLimit设置为1,但没有任何效果。
问题是如何确保应用程序正确地使用此配置,并且不会出现任何错误?
发布于 2022-05-18 00:43:53
Serilog有一个SelfLog特性,允许您输出有关Serilog配置的诊断信息。您可以找到有关此功能这里的更多信息。
#tl;dr你可以输出这样的诊断信息。
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
https://stackoverflow.com/questions/72206375
复制相似问题