【发布时间】: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