带有MSSQLSERVER接收器和AppSettings的自定义列

发布于 2025-01-24 17:12:52 字数 1417 浏览 3 评论 0原文

我有一个应用程序,可以在文件和控制台接收器上没有问题,现在我正在尝试添加MSSQLSERVER接收器。

查看 github 我有我的应用程序写信给我的申请书SQL数据库以及其他下沉。

    <add key="serilog:write-to:MSSqlServer.connectionString" value="Server=servername;Database=databasename;User Id=userid;Password=password;"/>
    <add key="serilog:write-to:MSSqlServer.tableName" value="Logs"/>
    <add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="true"/>

一个改进是我想在Serilog用来存储一个数字的日志表中添加一个自定义列,以指示我的应用程序唯一的RunID。一个想法是,一个简单的查询将允许RunID通过RunID进行分组,以查看该运行的所有消息。

因此,我根据文档(并且找不到其他任何示例)添加了以下内容,因为它似乎是合乎逻辑的:

    <add key="serilog:write-to:MSSqlServer.columnOptions.ColumnName" value="RunId"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.PropertyName" value="RunId"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.DataType" value="SqlDbType.Int"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.DataLength" value="32"/>

然后在我的代码中,我需要做的就是:

    Log.Information("{RunId}{Message}", RunId, Message);

要查看带有{runid}的新条目在“ runid”列和{message}中的{message}中...但是,每次我执行此操作时都没有写入RunID列,它保持为空,而控制台/文件的每个日志消息也都在表中重复。

因此,看来记录在起作用,这一定是钥匙错误,我真的不确定应该使用什么。

有人可以将我指向我需要走的方向或我出了什么问题吗?

谢谢。

I've got an application that runs without problem with the File and Console Sinks and now I'm trying to add the MSSqlServer Sink.

Looking at the documentation on Github I've got my application to write to the SQL Database as well as the other sinks.

    <add key="serilog:write-to:MSSqlServer.connectionString" value="Server=servername;Database=databasename;User Id=userid;Password=password;"/>
    <add key="serilog:write-to:MSSqlServer.tableName" value="Logs"/>
    <add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="true"/>

One improvement is I'd like to add a custom column to the Logs table that Serilog uses to store a number indicating a unique RunId for my application. The idea being that a simple query would allow grouping by RunId to see all messages for that one run.

So I added the following, based on the documentation (and I haven't been able to find any other examples) as it seemed logical:

    <add key="serilog:write-to:MSSqlServer.columnOptions.ColumnName" value="RunId"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.PropertyName" value="RunId"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.DataType" value="SqlDbType.Int"/>
    <add key="serilog:write-to:MSSqlServer.columnOptions.DataLength" value="32"/>

and then in my code all I need to do is:

    Log.Information("{RunId}{Message}", RunId, Message);

to see a new entry with {RunId} in the RunId column and {Message} in the Message column... however everytime I do this nothing is written to the RunId column, it remains as NULL whereas every log message the console/file has is also duplicated in the Table.

So it seems logging is working, it must be the keys wrong and I'm really not sure what should be used.

Would anyone be able to point me in the direction I need to be going or where I've gone wrong?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-01-31 17:12:52

登录肯定在工作,但是有些挖掘,最后我发现我有两个问题:

  1. 在数据库上使用了用户Serilog正在使用的不足的权限,而
  2. 我使用的AppSettings设置是错误的

#1很容易修复,我创建了一个专用的Serilog说明此数据库并根据文档修复了此数据库。

#2然而,非常令人沮丧,但最终我能够使以下内容有效:

    <configSections>
    <section name="MSSqlServerSettingsSection" type="Serilog.Configuration.MSSqlServerConfigurationSection, Serilog.Sinks.MSSqlServer"/>
</configSections>
<MSSqlServerSettingsSection DisableTriggers="false" ClusteredColumnstoreIndex="false" PrimaryKeyColumnName="Id">
    <!-- SinkOptions parameters -->
    <TableName Value="Logs"/>
    <Columns>
        <add ColumnName="RunId" DataType="int"/>
    </Columns>
</MSSqlServerSettingsSection>

在编译和执行时,我的程序现在将在数据库中创建日志表,然后创建一个RunID列,然后我能够使用{RunID {RunID填充它}在我的log.debug()调用中表达式。

FWIW:我希望这会有所帮助,如果我能弄清楚我将如何查看是否可以将其添加到文档中,以此作为使用AppSetting的示例。搜索这个问题,大多数人似乎都在使用JSON而不是AppSettings。

Logging definitely was working but some digging and finally I found out I had two issues:

  1. Permissions on database where insuffident for the user serilog was using, and
  2. AppSettings settings I was using were wrong

#1 was easy enough to fix, I created a dedicated Serilog account for this database and fixed the permissions as per documentation.

#2 however was very frustrating but eventually I was able to get the following to work:

    <configSections>
    <section name="MSSqlServerSettingsSection" type="Serilog.Configuration.MSSqlServerConfigurationSection, Serilog.Sinks.MSSqlServer"/>
</configSections>
<MSSqlServerSettingsSection DisableTriggers="false" ClusteredColumnstoreIndex="false" PrimaryKeyColumnName="Id">
    <!-- SinkOptions parameters -->
    <TableName Value="Logs"/>
    <Columns>
        <add ColumnName="RunId" DataType="int"/>
    </Columns>
</MSSqlServerSettingsSection>

On compile and execution my program will now create the Logs table in the database and then create a RunId column which I have been able to populate with a {RunId} expression in my Log.Debug() calls.

FWIW: I hope this will be helpful and if I can work out how I'll see if I can add this to documentation as an example of using AppSettings for this use case. Searching this question most people seem to be using JSON and not AppSettings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文