将自定义列添加到 Azure Table 存储中的 Serilog Table 以记录新信息
Add a custom column to Serilog Table in Azure Table Storage for logging new information
可以在 Azure 的 Serilog Table 中添加新列 table 用于记录新的额外字段(如帐户 ID 或登录名)的存储?
我认为可以添加一个新列,但是可以像我对新添加的列所说的那样在 Serilog 中传递额外的字段吗?我如何在 Startup.cs 或 web.config 中定义它?谢谢
这是我在web.config中的配置:
<add key="serilog:using:AzureTableStorage" value="Serilog.Sinks.AzureTableStorage" />
<add key="serilog:write-to:AzureTableStorageWithProperties.connectionString" value="DefaultEndpointsProtocol=https;AccountName=MyAccountName;AccountKey=MyAccountKey;EndpointSuffix=core.windows.net" />
<add key="serilog:write-to:AzureTableStorage.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
<add key="serilog:write-to:AzureTableStorageWithProperties.storageTableName" value="Serilog" />
startup.cs配置:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();
如果你想在使用 Serilog 将日志写入 azure table 存储时添加额外的属性,你需要调用 Enrich.FromLogContext()
和 LogContext.PushProperty
。使用这两种方法,您的应用程序将向 Azure table 添加额外的属性。详情请参考document
例如
- 安装SDK
Install-Package Serilog.Sinks.AzureTableStorage
Install-Package Serilog.Enrichers.Thread
- 代码
static void Main(string[] args)
{
var storage =CloudStorageAccount.Parse("");
string tableName = "log";
var _log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.AzureTableStorageWithProperties(storage, LogEventLevel.Information, storageTableName: tableName, propertyColumns: new string[] { "AccountId" , "Name" }) ;
var logger = _log.CreateLogger();
var exampleuser = new User { AccountId = 3, Name = "Allen" };
LogContext.PushProperty("AccountId", exampleuser.AccountId);
logger.Information("{@User}", exampleuser);
exampleuser = new User { AccountId = 1, Name = "Jim" };
LogContext.PushProperty("AccountId", exampleuser.AccountId);
logger.Information("{@User}", exampleuser);
Console.ReadLine();
}
class User
{
public int AccountId { get; set; }
public string Name { get; set; }
}
可以在 Azure 的 Serilog Table 中添加新列 table 用于记录新的额外字段(如帐户 ID 或登录名)的存储?
我认为可以添加一个新列,但是可以像我对新添加的列所说的那样在 Serilog 中传递额外的字段吗?我如何在 Startup.cs 或 web.config 中定义它?谢谢
这是我在web.config中的配置:
<add key="serilog:using:AzureTableStorage" value="Serilog.Sinks.AzureTableStorage" />
<add key="serilog:write-to:AzureTableStorageWithProperties.connectionString" value="DefaultEndpointsProtocol=https;AccountName=MyAccountName;AccountKey=MyAccountKey;EndpointSuffix=core.windows.net" />
<add key="serilog:write-to:AzureTableStorage.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
<add key="serilog:write-to:AzureTableStorageWithProperties.storageTableName" value="Serilog" />
startup.cs配置:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();
如果你想在使用 Serilog 将日志写入 azure table 存储时添加额外的属性,你需要调用 Enrich.FromLogContext()
和 LogContext.PushProperty
。使用这两种方法,您的应用程序将向 Azure table 添加额外的属性。详情请参考document
例如
- 安装SDK
Install-Package Serilog.Sinks.AzureTableStorage
Install-Package Serilog.Enrichers.Thread
- 代码
static void Main(string[] args)
{
var storage =CloudStorageAccount.Parse("");
string tableName = "log";
var _log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.AzureTableStorageWithProperties(storage, LogEventLevel.Information, storageTableName: tableName, propertyColumns: new string[] { "AccountId" , "Name" }) ;
var logger = _log.CreateLogger();
var exampleuser = new User { AccountId = 3, Name = "Allen" };
LogContext.PushProperty("AccountId", exampleuser.AccountId);
logger.Information("{@User}", exampleuser);
exampleuser = new User { AccountId = 1, Name = "Jim" };
LogContext.PushProperty("AccountId", exampleuser.AccountId);
logger.Information("{@User}", exampleuser);
Console.ReadLine();
}
class User
{
public int AccountId { get; set; }
public string Name { get; set; }
}