在AppSettings.json中为Serilog接收器MsSqlServer配置自定义列选项不起作用 [英] Configure custom Column Options for Serilog Sinks MsSqlServer in AppSettings.json is not working
问题描述
我正在尝试确定是否可以为ASP.Net Core 3项目的 appsettings.json
文件中的serilog sink mssqlserver配置列选项。 / p>
除了不填充自定义列外,数据库也以标准方式创建而没有这些列,因此我手动添加了它们。这可能指向配置问题?
创建表的SQL
创建表[dbo] 。[logtable](
[Id] [int] IDENTITY(1,1)NOT NULL,
[Message] [nvarchar](max)NULL,
[MessageTemplate] [nvarchar]( max)NULL,
[Level] [nvarchar](128)NULL,
[TimeStamp] [datetimeoffset](7)NOT NULL,
[Exception] [nvarchar](max)NULL,
[属性] [xml] NULL,
[LogEvent] [nvarchar](max)NULL,
[Controller_Name] [varchar](500)NULL,
[Method_Name] [ varchar](500)NULL,
[StackTrace] [varchar](500)NULL
)
我在 Program.cs
文件中创建和配置记录器。
公共静态IWebHost BuildWebHost(string [] args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup< Startup>()
.UseSerilog()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
我可以从 appsettings.json构建配置文件
文件,其中包含一个Serilog节点,其中包含要使用的连接字符串和表的信息。
Serilog:{
使用:[ Serilog.Sinks.MSSqlServer],
MinimumLevel:信息,
WriteTo:[
{
Name: MSSqlServer,
Args:{
connectionString:数据源= SWPC159;初始目录= master; Trusted_Connection = True;,//连接字符串
tableName: logtable,
columnOptionsSection:{
disableTriggers:true,
clusteredColumnstoreIndex:false,
addStandardColumns:[ LogEvent ],
removeStandardColumns:[ MessageTemplate,属性],
additionalColumns:[
{
ColumnName: Controller_Name,
DataType: varchar,
AllowNull:true,
DataLength:500
},
{
ColumnName: Method_Name,
DataType: varchar,
AllowNull:true,
DataLength:500
},
{
ColumnName: StackTrace,
DataType: varchar,
AllowNull:true,
DataLength:500
}
]
}
}
}
]
}
用宿主上下文替换配置,解决问题
公共静态IWebHost BuildWebHost(string [] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup< Startup>()
.UseSerilog(( HostingContext,loggerConfiguration)=> loggerConfiguration.ReadFrom
.Configuration(hostingContext.Configuration))
.Build();
}
I'm trying to determine if it's possible to configure the column options for serilog sink mssqlserver in the appsettings.json
file for an ASP.Net Core 3 project.
besides not filling the custom columns, the database is also created in the standard way without these column, so I added them by hand. This could point to a configuration issue?
The SQL to create the table
CREATE TABLE [dbo].[logtable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Message] [nvarchar](max) NULL,
[MessageTemplate] [nvarchar](max) NULL,
[Level] [nvarchar](128) NULL,
[TimeStamp] [datetimeoffset](7) NOT NULL,
[Exception] [nvarchar](max) NULL,
[Properties] [xml] NULL,
[LogEvent] [nvarchar](max) NULL,
[Controller_Name] [varchar](500) NULL,
[Method_Name] [varchar](500) NULL,
[StackTrace] [varchar](500) NULL
)
I create and configure the logger in the Program.cs
file.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
I can build the configuration file from the appsettings.json
file, which contains a Serilog node with information for which connection string and table to use.
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=SWPC159; Initial Catalog=master;Trusted_Connection=True;", // connection String
"tableName": "logtable",
"columnOptionsSection": {
"disableTriggers": true,
"clusteredColumnstoreIndex": false,
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
"additionalColumns": [
{
"ColumnName": "Controller_Name",
"DataType": "varchar",
"AllowNull": true,
"DataLength": 500
},
{
"ColumnName": "Method_Name",
"DataType": "varchar",
"AllowNull": true,
"DataLength": 500
},
{
"ColumnName": "StackTrace",
"DataType": "varchar",
"AllowNull": true,
"DataLength": 500
}
]
}
}
}
]
}
Replacing configuartion with hosting context,solve the issues
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration.ReadFrom
.Configuration(hostingContext.Configuration))
.Build();
}
这篇关于在AppSettings.json中为Serilog接收器MsSqlServer配置自定义列选项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!