在AppSettings.json中为Serilog接收器MsSqlServer配置自定义列选项不起作用

编程入门 行业动态 更新时间:2024-10-24 16:25:01
本文介绍了在AppSettings.json中为Serilog接收器MsSqlServer配置自定义列选项不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试确定是否可以为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配置自定义列选项不起作用

本文发布于:2023-11-14 11:35:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   自定义   中为   选项   不起作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!