[英]Populate custom columns in Serilog MsSql sink
I am using Serilog in my dotnet core application.我在我的 dotnet 核心应用程序中使用 Serilog。 I have added custom columns to the default list of columns provided by Serilog.
我已将自定义列添加到 Serilog 提供的默认列列表中。 Below is how my "Serilog" configuration looks like in appsettings.json file -
下面是我的“Serilog”配置在 appsettings.json 文件中的样子 -
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": <connectionString>
"tableName": "Log",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Properties"], //remove the Properties column in the standard ones
"customColumns": [
{
"ColumnName": "ControllerName",
"DataType": "varchar",
"DataLength": 50
}
]
},
"timeStamp": {
"columnName": "Timestamp",
"convertToUtc": true
}
}
}
]
}
So I have removed "MessageTemplate" and "Properties" from the default list of columns created by Serilog and added "ControllerName" as a new column to the table
Log , where Serilog logs its data.
因此,我从 Serilog 创建的默认列列表中删除了“MessageTemplate”和“Properties”,并将“ControllerName”作为新列添加到表
Log中,Serilog 在其中记录其数据。 What i want is that when I am logging information , I want to provide value to the "ControllerName" column.
我想要的是,当我记录信息时,我想为“ControllerName”列提供值。 How can it be done?
怎么做到呢? I have found the below solution :
我找到了以下解决方案:
_logger.LogInformation("{ControllerName}{Message}", "TestController", "Starting up..");
This line of code provides value to the ControllerName column, but the message column gets the value as这行代码为 ControllerName 列提供了值,但消息列将值作为
"TestController""Starting up.."
I want the message column to get value as我希望消息列获得值
Starting up..
It seems you are using Microsoft's ILogger<T>
instead of Serilog's ILogger
thus in order to add a contextual property that will be included in your log event without being part of the message, you have to create a new logging scope using BeginScope
eg您似乎使用的是 Microsoft 的
ILogger<T>
而不是 Serilog 的ILogger
,因此为了添加将包含在日志事件中而不是消息的一部分的上下文属性,您必须使用BeginScope
创建一个新的日志记录范围,例如
using (_logger.BeginScope("{ControllerName}", nameof(TestController)))
{
_logger.LogInformation("{Message}", "Starting up...");
}
Another alternative is to add a property to Serilog's LogContext
:另一种选择是向 Serilog 的
LogContext
添加一个属性:
using (LogContext.PushProperty("ControllerName", nameof(TestController))
{
_logger.LogInformation("{Message}", "Starting up...");
}
This will give you the same end result as with BeginScope
above, but is a Serilog-specific API and kind of defeats the purpose of using ILogger<T>
in the first place, so BeginScope
would be preferred unless you decide to use Serilog's ILogger
instead.这将为您提供与上述
BeginScope
相同的最终结果,但它是特定于 Serilog 的 API,并且首先违背了使用ILogger<T>
的目的,因此除非您决定改用 Serilog 的ILogger
,否则BeginScope
将是首选.
One important observation is that in order for the LogContext
to work, you need to enable it when you configure your logger.一项重要的观察是,为了使
LogContext
工作,您需要在配置记录器时启用它。 For example:例如:
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext() // <<<<<<<<<<#############
.WriteTo.Console()
.CreateLogger();
If you were to use Serilog's ILogger
, then in addition to be able to use the LogContext
you can alternatively create a new context using Log.ForContext
:如果您要使用 Serilog 的
ILogger
,那么除了能够使用LogContext
,您还可以使用Log.ForContext
创建一个新的上下文:
var contextLogger = logger.ForContext("ControllerName", nameof(TestController));
contextLogger.Information("{Message}", "Starting up...");
ps: If you're not sure whether you should use Microsoft's ILogger<T>
or Serilog's ILogger
, I recommend reading this answer: Serilog DI in ASP.NET Core, which ILogger interface to inject? ps:如果您不确定是否应该使用微软的
ILogger<T>
或 Serilog 的ILogger
,我建议阅读这个答案: Serilog DI in ASP.NET Core, which ILogger interface to injection?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.