gpt4 book ai didi

asp.net-core - 利用 Serilog.AspNetCore 和 AutofacSerilogIntegration 通过 AspNetCore WebApp 从 .NET Core 类库解析 Microsoft.Extensions.Logging.ILogger

转载 作者:行者123 更新时间:2023-12-01 19:44:15 25 4
gpt4 key购买 nike

我遇到以下异常:

Cannot resolve parameter 'Microsoft.Extensions.Logging.ILogger logger'

"At the moment (9/28/18), Serilog.AspNetCore doesn't get involved in DI/OoC configuration"

是否可以在 Autofac DI 容器中将 Serilog.Core.Logger 注册为 Microsoft.Extensions.Logging.ILogger 的实现,以便在 .NET Core 类中进行解析图书馆?

这是我当前的实现:

public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
//.WriteTo.MSSqlServer(Configuration.GetConnectionString("LogConnection"), "_logs", columnOptions: columnOptions)
.WriteTo.Console()
//.WriteTo.ApplicationInsightsEvents("<YOUR_APPLICATION_INSIGHTS_KEY>")
.CreateLogger();
try
{
Log.Information("Starting web host");

var host = CreateWebHostBuilder(args).Build();
host.Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog();
}

public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
_builder.Register<ILogger>((c, p) =>
{
return new LoggerConfiguration()
.WriteTo.RollingFile(
AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/Log-{Date}.txt")
.CreateLogger();
}).SingleInstance();
}

}

最佳答案

尝试解决 ILogger<T>而不是ILogger 。默认日志工厂仅注册通用版本。

关于asp.net-core - 利用 Serilog.AspNetCore 和 AutofacSerilogIntegration 通过 AspNetCore WebApp 从 .NET Core 类库解析 Microsoft.Extensions.Logging.ILogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com