c# - Serilog Net Core 附加列问题

标签 c# sql-server .net-core serilog

我正在为 .NET Core 2 使用 Serilog SQL Server Sink。

我已经在 Program.csStartup.cs 中配置了 Serilog.Logger,如下所示:

ColumnOptions columnOptions = new ColumnOptions();

columnOptions.AdditionalDataColumns = new System.Collections.Generic.List<DataColumn>();
columnOptions.AdditionalDataColumns.Add(new DataColumn() { ColumnName = "UserId", DataType = typeof(string) });            
Log.Logger = new LoggerConfiguration()               
   .WriteTo.MSSqlServer("Data Source=TS-ABC\\SQLExpress;Initial Catalog=XYZ;Integrated Security=True",
    "ActivityLogs", columnOptions: columnOptions, autoCreateSqlTable: false, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug)
.CreateLogger();

Sstartup.cs中:

services.AddSingleton<Serilog.ILogger>(x =>
{
    ColumnOptions columnOptions = new ColumnOptions();

    columnOptions.AdditionalDataColumns = new System.Collections.Generic.List<DataColumn>();                
    columnOptions.AdditionalDataColumns.Add(new DataColumn() { ColumnName = "UserId", DataType = typeof(string) });

    return new LoggerConfiguration().WriteTo.MSSqlServer(Configuration["Data:DefaultConnection:ConnectionString"], Configuration["Serilog:TableName"], autoCreateSqlTable: false, columnOptions: columnOptions, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug).CreateLogger();
});

UserIdstring 类型并允许空值。它也是 ActivityLogs 表中的 FK。

这就是我记录信息的方式:

Log.Information("This has properties attached");

只要添加附加列 "UserId",代码就可以正常工作。 UserId 列已存在于我的数据库中。

这是我在 Visual Studio 的输出窗口中看到的错误:

Unable to write 50 log events to the database due to following error: The given key was not present in the dictionary.

更新到 Serilog.Sinks.MSSqlServer nuget 包后,我现在收到以下错误并且数据库中没有任何记录。

Unable to write 1 log events to the database due to following error: The given ColumnMapping does not match up with any column in the source or destination.

任何解决方案、提示或解决方法将不胜感激。

最佳答案

.net core 的不可空附加列的问题。将您的字段设置为 Nullable。

关于c# - Serilog Net Core 附加列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49030633/

相关文章:

c# - "Replace with single call to single"是什么意思?

c# - 奇怪的 xml c# 问题,Vista 是否缓存程序文件?

sql-server - 解决 SqlBulkCopy 没有执行最少日志记录的问题

sql-server - 关于 2 个表之间的 SQL Server 关系的 Noddy 问题。你能澄清一下吗?

azure - 如何在 Azure Devops 上安装多个 .NET Core SDK 版本

asp.net-core - .NET Core 托管包

c# - 作为 dotnet cli 工具启动时获取 "System.Data.SqlClient is not supported on this platform"

c# - 在哪里设置 <gcAllowVeryLargeObjects>?

c# - 数据未使用 TcpClient 在 LAN 上完全传输

sql-server - 无法获得约束以在 ForEach 循环中处理 SQL 任务