我正在尝试将自定义列添加到我的日志记录数据库,但是我得到了 TargetInvocationException。
它在我添加之前工作MSSqlServerSettingsSection
(因此没有自定义列),所以我可能在那里遗漏了一些东西。
但是,查看https://github.com/serilog/serilog-sinks-mssqlserver上的示例,我无法弄清楚我需要什么。
MSSqlServer 接收器设置:
<MSSqlServerSettingsSection>
<Columns>
<add ColumnName="JobId"
DataType="nvarchar"
DataLength="256"
AllowNull="True"/>
</Columns>
</MSSqlServerSettingsSection>
应用程序配置:
<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),
)
DI 使用统一:
container.RegisterInstance<ILogger>(new LoggerConfiguration()
.Enrich
.FromLogContext()
.ReadFrom
.AppSettings()
.CreateLogger());
Log.Logger = container.Resolve<ILogger>();
慕雪6442864
相关分类