【问题标题】:When implement Serilog debug mode does not work [.NET Core 3.1]当实现 Serilog 调试模式不起作用 [.NET Core 3.1]
【发布时间】:2020-06-13 11:13:06
【问题描述】:

第一次实现 Serilog 时,dotnet core 版本是 2.2。然后我们将应用程序版本更新为 3.1。后来看到,当我启动调试模式时,chrome页面没有启动。

我的第一个实现是:

程序.cs:

public static void Main(string[] args)
{       

    var sinkOpts = new SinkOptions { TableName = "_Log", AutoCreateSqlTable = true, BatchPostingLimit = 10 };    

    var columnOptions = new ColumnOptions
    {
        AdditionalColumns = new Collection<SqlColumn>
        {
            new SqlColumn
                {ColumnName = "UserName", PropertyName = "UserName", DataType = SqlDbType.NVarChar, DataLength = 64},
        }
    };

    Log.Logger = new LoggerConfiguration()            
            .WriteTo.MSSqlServer(
                connectionString: appSettings.GetConnectionString("DB"),
                sinkOptions: sinkOpts,
                columnOptions: columnOptions,
                restrictedToMinimumLevel: LogEventLevel.Error
            ).CreateLogger();           
}

然后我创建了一个中间件,用于从 HttpContext 中获取一些信息:

public class LogContextMiddleware
{
    private readonly RequestDelegate next;

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

    public Task Invoke(HttpContext context)
    {
        LogContext.PushProperty("UserName", context.User.Identity.Name);

        return next(context);
    }
}

最后在 Startup.cs 使用这个中间件

app.UseStaticFiles();
app.UseMiddleware<LogContextMiddleware>(); <--
app.UseHttpsRedirection(); 

就是这样...这项工作在 2.2 完全可以工作。官方文档说在 Startup.cs 添加“UseSerilogRequestLogging()”但不工作......我在哪里失踪?

【问题讨论】:

    标签: asp.net-core .net-core serilog


    【解决方案1】:

    启动App时出现错误提示? 连接字符串没问题?

    考虑 app.UseSerilogRequestLogging 或 app.UseMiddleware();在 Configure(..) 方法中,顺序很重要

    最后,在Program.cs上配置web主机的时候,你是不是调用了“.UseSerilog()”方法?否则,它将不起作用。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 2020-09-04
      • 2021-03-09
      • 2023-03-19
      • 2022-07-06
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多