2 回答
TA贡献1854条经验 获得超8个赞
要“静默”ASP.NET 默认日志,您需要将其添加到 appsettings.json 中:
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"System": "Warning"
}
}
那么默认记录器将只记录“Warning”或更低级别的所有内容(有 6 个级别的日志:Trace、Debug、Information、Warning、Error Critical)。另外,为了启用 Serilog,您只需要添加此包:
<PackageReference Include="Serilog.AspNetCore" Version="#version#" />
并将其添加到您的Program.cs:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog(); //this line
然后你可以简单地ILogger<T>使用 DI 注入,它将使用 Serilog 与你的配置。此外,它还会封装您的 Serilog,以便以后更容易使用其他日志框架
更新:为了删除以前配置的提供程序,请尝试添加此行:
new WebHostBuilder()
.ConfigureLogging(builder => builder.ClearProviders()) // <--here
TA贡献1846条经验 获得超7个赞
就我而言,我必须在Startup.cs/Configure()方法中从 Core 库设置 Serilog 的 ILogger。
程序.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddAutofac())
.UseStartup<Startup>()
.UseSerilog(); // Added this line as per docs
启动.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IZLogger zLogger)
{
...
Log.Logger = zLogger.GetCurrentClassLogger<Startup>(); // Set Serilog's ILogger from Core library
app.UseSerilogRequestLogging(); // Added this line as per docs
...
}
日志示例:
2019-07-22 19:01:03.179 -04:00 | [INFO] | Request starting HTTP/1.1 GET https://localhost:5001/favicon.ico
2019-07-22 19:01:03.180 -04:00 | [INFO] | HTTP GET /favicon.ico responded 404 in 0.394846 ms
2019-07-22 19:01:03.181 -04:00 | [INFO] | Request finished in 1.8292ms 404 text/plain
- 2 回答
- 0 关注
- 156 浏览
添加回答
举报