问题
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
}
]
}
}
}
]
}
回答1:
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();
}
来源:https://stackoverflow.com/questions/60428660/configure-custom-column-options-for-serilog-sinks-mssqlserver-in-appsettings-jso