[英]How can I add a custom column to my Serilog MS SQL Server sink?
I am trying to add a custom column to my logging database, however I get a TargetInvocationException.我正在尝试将自定义列添加到我的日志记录数据库,但是我收到了 TargetInvocationException。
It was working before I added MSSqlServerSettingsSection
(so without the custom column), so I'm probably missing something there.在我添加
MSSqlServerSettingsSection
(所以没有自定义列)之前它正在工作,所以我可能在那里遗漏了一些东西。
However, looking at the example at https://github.com/serilog/serilog-sinks-mssqlserver I can't figure out what I need.但是,查看https://github.com/serilog/serilog-sinks-mssqlserver上的示例,我无法弄清楚我需要什么。
MSSqlServer sink settings: MSSqlServer 接收器设置:
<MSSqlServerSettingsSection>
<Columns>
<add ColumnName="JobId"
DataType="nvarchar"
DataLength="256"
AllowNull="True"/>
</Columns>
</MSSqlServerSettingsSection>
app.config:应用程序配置:
<configuration>
<configSections>
<section name="MSSqlServerSettingsSection" type="Serilog.Configuration.MSSqlServerConfigurationSection, Serilog.Sinks.MSSqlServer"/>
</configSections>
<MSSqlServerSettingsSection configSource="Configs\Shared.Serilog.MSSqlServerSettings.config" />
<appSettings>
<add key="serilog:using:MSSqlServer" value="Serilog.Sinks.MSSqlServer" />
<add key="serilog:write-to:MSSqlServer.connectionString" value="Server=127.0.0.1; Database=LogDB; User Id=Serilog; Password=Password123;" />
<add key="serilog:write-to:MSSqlServer.tableName" value="Logs" />
<add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="false" />
<add key="serilog:write-to:MSSqlServer.restrictedToMinimumLevel" value="Verbose" />
</appSettings>
</configuration>
Creation query:创建查询:
CREATE TABLE [dbo].[Logs]
(
[Id] int IDENTITY(1,1) NOT NULL,
[Message] nvarchar(max) NULL,
[MessageTemplate] nvarchar(max) NULL,
[Level] nvarchar(128) NULL,
[TimeStamp] datetime NOT NULL,
[Exception] nvarchar(max) NULL,
[Properties] nvarchar(max) NULL,
[JobId] nvarchar(256) NULL
CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED ([Id] ASC),
)
DI using unity:使用 unity 的 DI:
container.RegisterInstance<ILogger>(new LoggerConfiguration()
.Enrich
.FromLogContext()
.ReadFrom
.AppSettings()
.CreateLogger());
Log.Logger = container.Resolve<ILogger>();
I have tried to debug with Selflog, but the log file is empty:我尝试使用 Selflog 进行调试,但日志文件为空:
var file = File.CreateText("Self.log");
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
I'm getting the following exception我收到以下异常
System.Configuration.ConfigurationErrorsException: 'Unrecognized attribute
'DataLength'. Note that attribute names are case-sensitive.'
Not sure why this is, according to the documentation it should be correct.不知道为什么会这样,根据文档,它应该是正确的。 Removing this line gave me the same issue on 'AllowNull' .
删除这条线给了我同样的问题 'AllowNull' 。
Removing both lines seems to work.删除两条线似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.