我是编程新手,我正在尝试为.net核心项目实现日志记录解决方案。
我要收集默认和Microsoft类别日志。
APPSETTING.JSON
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Information"
},
"File": {
"location": "logs/timeapi.log"
}
},
"EnableSwagger": true
}
PROGRAM.CS
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilogRequestLogging();
STARTUP.CS
app.UseSerilogRequestLogging(options =>
options.RequestProjection =
r => new { r.IsHttps, QueryString = r.QueryString.Value });
app.UseMvc();
https://github.com/mthamil/AspNetCore.Serilog.RequestLoggingMiddleware
我得到的错误是:
Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AspNetCore.Serilog.RequestLoggingMiddleware.SerilogRequestMiddleware'.
答案 0 :(得分:1)
您使用的中间件似乎需要在应用程序的Startup.ConfigureServices()
method中添加Serilog的ILogger
;例如:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILogger>(Log.Logger);
}
(此示例假定您正在配置Serilog的Log.Logger
静态属性。)
在Serilog.AspNetCore package的自述文件中,有一个使用Serilog设置请求日志的更完整的示例,尽管其对UseSerilogRequestLogging()
的实现却大不相同。
答案 1 :(得分:1)
如果您使用的是.net core 3.1 +,请仅在program.cs中使用它,并从startup.cs中删除有关Serilog的所有内容
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.WriteTo.Console())
......;
答案 2 :(得分:0)