我试图将自定义列添加到我的日志数据库,但是我得到了一个TargetInvocationException。
它在我添加MSSqlServerSettingsSection
之前工作(所以没有自定义列),所以我可能在那里遗漏了一些东西。
然而,看看https://github.com/serilog/serilog-sinks-mssqlserver的例子,我无法弄清楚我需要什么。
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>
创建查询:
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),
)
使用统一:
container.RegisterInstance<ILogger>(new LoggerConfiguration()
.Enrich
.FromLogContext()
.ReadFrom
.AppSettings()
.CreateLogger());
Log.Logger = container.Resolve<ILogger>();
我尝试使用Selflog进行调试,但日志文件为空:
var file = File.CreateText("Self.log");
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
我收到以下异常
System.Configuration.ConfigurationErrorsException: 'Unrecognized attribute
'DataLength'. Note that attribute names are case-sensitive.'
不知道为什么会这样,根据文档它应该是正确的。删除此行在“AllowNull”上给出了同样的问题。
删除两行似乎都有效。