简体   繁体   English

如何将自定义列添加到我的 Serilog MS SQL Server 接收器?

[英]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.

相关问题 如何将完整的对象添加到 Serilog Azure Tablestorage 接收器,该对象未在消息中写出? - How can I add a complete object to Serilog Azure Tablestorage sink, which is not written out in the message? Serilog SQL Server接收器日志记录未记录任何错误 - No errors logged for Serilog SQL server sink logging 如何在 SeriLog Sink 中获取当前的 HttpContext? - How can I get the current HttpContext in a SeriLog Sink? 如何解决 serilog 自定义接收器中的依赖关系? - How to resolve dependency in serilog custom sink? 如何将自定义文件名添加到 serilog 文件接收器生成的日志文件 - How to add custom file name to serilog file sink generated logs file 是否有任何SQL Server Serilog接收器来支持.Net Core - Is there any SQL Server Serilog sink to support .Net Core Serilog MSSqlServer接收器是否应该与Azure SQL Server一起使用? - Is Serilog MSSqlServer sink supposed to work with Azure SQL Server? 自定义属性的 Serilog 过滤器接收器 - Serilog Filter Sink by Custom Property 不能为 serilog 配置自定义接收器 - Сan't configure a custom sink for a serilog 如何在项目设置中添加SQL Server数据库 - How can I add SQL Server Database in my project setup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM