![](/img/trans.png)
[英]Using Serilog for Logging with Azure Table Storage in .NET Core MVC
[英]Serilog Azure Table Storage and context properties
我正在尝试在Serilog.Sinks.AzureTableStorage 中使用 AzureTableStorageWithProperties AzureTableStorageWithProperties
并且我有一个附加到上下文的属性; 是这样的:
using (LogContext.PushProperty("CameraId", camera.Id)
{
/* ... */
}
所以,我想知道如何配置记录器以考虑此属性并在它出现在上下文中时为其创建一个列。
我试过这个,但它似乎没有用:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.AzureTableStorageWithProperties(
"my-connection-string",
storageTableName: "mytable",
propertyColumns: new[] { "CameraId" })
.CreateLogger();
另外,您可能已经注意到,我正在使用Serilog.Settings.Configuration ,所以我希望您能告诉我如何将它也放入我的appsettings.json
中。
谢谢
我弄清楚了为什么看不到我的属性:我需要调用Enrich.FromLogContext()
,该属性显示在Azure表存储中。
我非常努力地了解如何在appsettings.json
指定propertyColumns
(或与此相关的环境变量),但是没有运气... :(
我得到它像这样工作:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Async", "Serilog.Sinks.AzureTableStorage" ],
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "AzureTableStorageWithProperties",
"Args": {
"connectionString": "UseDevelopmentStorage=true",
"storageTableName": "LogTable",
"propertyColumns": [ "CameraId" ]
}
}
]
}
}
],
"Enrich": [ "FromLogContext" ]
}
}
对于异步位,请使用:Serilog.Sinks.Async。
同样不要忘记在应用程序关闭时调用Log.CloseAndFlush();-)
我一直在寻找这个解决方案,因为我一直在寻找比 blob 具有更多结构的日志存储:这可行。 请注意,使用 appsettings.json 中的配置规则:您的解决方案仍然使用 program.cs 中的基本配置,代码如下:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.CreateLogger();
感谢您发布解决方案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.