![](/img/trans.png)
[英]ASP.NET Core Web API - Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AuthService'
[英]Unable to resolve service for type 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware"
我是編程新手,我正在嘗試為 .net 核心項目實現日志記錄解決方案。
我想收集默認和 Microsoft 類別日志。
APPSETTING.JSON
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Information"
},
"File": {
"location": "logs/timeapi.log"
}
},
"EnableSwagger": true
}
程序.CS
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilogRequestLogging();
啟動文件
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'.
您使用的中間件似乎需要在應用程序的Startup.ConfigureServices()
方法中添加 Serilog 的ILogger
; 例如:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILogger>(Log.Logger);
}
(此示例假設您正在配置 Serilog 的Log.Logger
靜態屬性。)
在Serilog.AspNetCore包的自述文件中有一個更完整的使用 Serilog 設置請求日志記錄的示例,盡管它的UseSerilogRequestLogging()
實現完全不同。
如果您使用的是 .net core 3.1 + 僅在 program.cs 中使用它,並從 startup.cs 中刪除有關 Serilog 的所有內容
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.WriteTo.Console())
......;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.