繁体   English   中英

ASP.NET Core Serilog没有将属性推送到其自定义列

[英]ASP.NET Core Serilog not pushing property to its custom column

我在appsettings.json安装了appsettings.json用于我的Serilog安装的设置

"Serilog": {
  "MinimumLevel": "Information",
  "Enrich": [ "LogUserName" ],
  "Override": {
    "Microsoft": "Critical"
  },
  "WriteTo": [
    {
      "Name": "MSSqlServer",
      "Args": {
        "connectionString": "Server=.\\SQLEXPRESS;Database=Apple;Trusted_Connection=True;MultipleActiveResultSets=true;,
        "schemaName": "Apple",
        "tableName": "EventLogs",
        "columnOptionsSection": {
          "customColumns": [
            {
              "ColumnName": "UserName",
              "DataType": "nvarchar",
              "DataLength": 256,
              "AllowNull": true
            }
          ]
        }
      }
    }
  ]
},

我还有一个名为LogUserName的自定义LogUserName ,它应该将用户用户名添加到db中名为UserName的列中。

这是更丰富的:

public class LogUserName
{
    private readonly RequestDelegate next;

    public LogUserName(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        LogContext.PushProperty("UserName", context.User.Identity.Name);
        await next(context);
        //return next(context);
    }
}

我已将.Enrich.FromLogContext()添加到我的Program.cs

到目前为止,我能够在属性标记中看到UserName ,但我无法将其推送到列。

编辑:

我将此模型用于我的日志到数据库:

public class EventLogs
{
    [Key]
    public int Id { get; set; }
    public string Level { get; set; }
    public DateTime TimeStamp { get; set; }
    public string UserName { get; set; }
    public string Properties { get; set; }
    public string LogEvent { get; set; }
    public string Exception { get; set; }
    public string Message { get; set; }
    public string MessageTemplate { get; set; }
}

您的浓缩器无效。 你混淆了两个概念。 将属性添加到日志上下文与创建实际的richr不同。 实际的richr应该实现ILogEventEnricher,如本例所示。

您实际创建的是ASP.NET中间件。 LogContext.PushProprety返回一个IDisposable,应该包含在using语句中,然后using语句块中的任何内容都应该具有带有附加属性的日志上下文。 文档中所示。

解决此问题的一种方法是从您的LogUserName配置中删除LogUserName 然后将您的中间件更改为:

public class LogUserNameMiddleware
{
    private readonly RequestDelegate next;

    public LogUserNameMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        using(LogContext.PushProperty("UserName", context.User.Identity.Name))
        {
            await next(context);
        }       
    }
}

请注意,如果您还没有这样做,您需要告诉ASP.NET Core有关中间件的信息。

public static class LogUserNameMiddlewareExtensions
{
    public static IApplicationBuilder UseLogUserNameMiddleware(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<LogUserNameMiddleware>();
    }
}

在Startup.Configure方法中,您可以将中间件添加到管道中:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseLogUserNameMiddleware();
        //your other middleware
    }
}

注意,如果没有登录用户,您可能希望清理中间件以进行处理,因此您不会收到NullReferenceException。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM