gpt4 book ai didi

c# - 无法使用 Serilog 登录 MS SQL

转载 作者:太空狗 更新时间:2023-10-29 23:50:06 28 4
gpt4 key购买 nike

我正在使用带有 MSSqlServer 接收器的 Serilog。尽管我遵循了 Serilog.Sinks.MSSqlServer 中提到的所有步骤我仍然无法在 SQL 表中记录任何消息。如果您能告诉我我遗漏或配置不正确的部分,我将不胜感激?

这是我项目的配置代码部分:

public ILogger Logger = null;

private ColumnOptions _columnOptions = new ColumnOptions
{
AdditionalDataColumns = new Collection<DataColumn>
{
new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "CreatedDate",DataType = typeof (DateTime)},
new DataColumn() { AllowDBNull = true, ColumnName = "StatusID",DataType = typeof (byte)},
new DataColumn() { AllowDBNull = true, ColumnName = "ModifiedBy",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "ModifiedDate",DataType = typeof (DateTime) },
new DataColumn() { AllowDBNull = true, ColumnName = "Version",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "SessionID", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "Username", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "IsAuthenticated", DataType = typeof(bool) },
new DataColumn() { AllowDBNull = true, ColumnName = "ClientIPAddress", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "ControllerName", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "ActionName", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "GetParameters", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "Request", DataType = typeof(string) },
},
};

Logger = new LoggerConfiguration().WriteTo.MSSqlServer(
connectionString: ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString(),
period: TimeSpan.Zero,
batchPostingLimit: 5,
autoCreateSqlTable: false,
tableName: "Logs",
restrictedToMinimumLevel: LogEventLevel.Verbose,
columnOptions: _columnOptions)
.CreateLogger();

这是我正在使用的消息模板:

public const string AuditMessageTemplate = "{SessionID}, {Username}, {IsAuthenticated}, {ClientIPAddress}, {ControllerName}, {ActionName}, {GetParameters}, {Request}, {CreatedBy}, {CreatedDate}, {StatusID}, {ModifiedBy}, {ModifiedDate}, {Version}";

为了测试它,我编写了以下代码:

for (int i = 0; i < 200; i++)
{
AuditLogger.Instance.Information(LoggerParameters.AuditMessageTemplate, auditLog.SessionID,auditLog.Username, auditLog.IsAuthenticated, auditLog.ClientIPAddress, auditLog.ControllerName,auditLog.ActionName, auditLog.GetParameters, auditLog.Request, auditLog.CreatedBy, auditLog.CreatedDate, auditLog.StatusID, auditLog.ModifiedBy, auditLog.ModifiedDate, auditLog.Version);
}

这里是一些运行时信息:

locals windows of visual studio

这是我正在使用的程序集:

  • Serilog 1.5.0.0
  • Serilog.FullNetFx 1.5.0.0
  • Serilog.Sinks.MSSqlServer 3.0.0.0

最佳答案

你好,

我在向数据库中的日志表添加其他列时遇到了同样的问题。我发现,在定义日期类型Guid的数据列对象时,它不会记录。一旦我将数据类型更改为字符串,它就可以正常工作。

例子:

new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (Guid) },

对比

new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (string) },

我知道这并不理想,因为我也想使用 Guid 而不是字符串。

关于c# - 无法使用 Serilog 登录 MS SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178415/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com