简体   繁体   English

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

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

I have this setup in appsettings.json for my Serilog installation 我在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
            }
          ]
        }
      }
    }
  ]
},

I also have a custom enricher called LogUserName that is supposed to add the users username to a column called UserName in the db. 我还有一个名为LogUserName的自定义LogUserName ,它应该将用户用户名添加到db中名为UserName的列中。

This is the enricher: 这是更丰富的:

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);
    }
}

I have added .Enrich.FromLogContext() to my Program.cs . 我已将.Enrich.FromLogContext()添加到我的Program.cs

So far I am able to see the UserName in the property tag but I am unable to push it to the column. 到目前为止,我能够在属性标记中看到UserName ,但我无法将其推送到列。

EDIT: 编辑:

I use this model for my logs to the database: 我将此模型用于我的日志到数据库:

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; }
}

Your enricher isn't valid. 您的浓缩器无效。 You're mixing up two concepts. 你混淆了两个概念。 Adding a property to the log context is not the same as creating an actual enricher. 将属性添加到日志上下文与创建实际的richr不同。 An actual enricher should implement ILogEventEnricher, as shown in this example . 实际的richr应该实现ILogEventEnricher,如本例所示。

What you actually created is ASP.NET Middleware. 您实际创建的是ASP.NET中间件。 The LogContext.PushProprety returns an IDisposable and should be wrapped in a using statement, and then anything inside the using statement block should have the log context with the additional property. LogContext.PushProprety返回一个IDisposable,应该包含在using语句中,然后using语句块中的任何内容都应该具有带有附加属性的日志上下文。 Like shown in the documentation . 文档中所示。

One way to fix this is to remove LogUserName from your enrichers configuration. 解决此问题的一种方法是从您的LogUserName配置中删除LogUserName Then change your middleware to this: 然后将您的中间件更改为:

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);
        }       
    }
}

Note you'll need tell ASP.NET Core about the Middleware, if you haven't already done so. 请注意,如果您还没有这样做,您需要告诉ASP.NET Core有关中间件的信息。

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

and in the Startup.Configure method, you can add the middleware to your pipeline: 在Startup.Configure方法中,您可以将中间件添加到管道中:

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

Note, you may want to clean up your middleware to handle if there isn't a logged in user, so you don't get a NullReferenceException. 注意,如果没有登录用户,您可能希望清理中间件以进行处理,因此您不会收到NullReferenceException。

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

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