gpt4 book ai didi

c# - 尝试激活 'Serilog.AspNetCore.RequestLoggingMiddleware' 时无法解析“Serilog.Extensions.Hosting.DiagnosticContext”的服务

转载 作者:行者123 更新时间:2023-12-05 01:57:47 28 4
gpt4 key购买 nike

当我想使用自定义 SerilogRequestLogging 作为中间件时,出现以下异常:

System.InvalidOperationException: Unable to resolve service for type'Serilog.Extensions.Hosting.DiagnosticContext' while attempting toactivate 'Serilog.AspNetCore.RequestLoggingMiddleware'.

外部库中的自定义 SerilogRequestLogging:

   public static IApplicationBuilder UseCustomSerilogRequestLogging(this IApplicationBuilder app)
{
return app.UseSerilogRequestLogging(options =>
{
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{

diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
diagnosticContext.Set("RequestMethod", httpContext.Request.Method);
diagnosticContext.Set("RequestProtocol", httpContext.Request.Protocol);
diagnosticContext.Set("RequestPath", httpContext.Request.Path);
diagnosticContext.Set("RequestRemoteAddress", httpContext.Connection.RemoteIpAddress);

};
});
}

然后我将它作为中间件添加到 Configure 方法中,如下所示:

 app.UseCustomSerilogRequestLogging();

在program.cs中:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseSerilog((provider, context, loggerConfig) =>
{
loggerConfig.Configure(provider, Configuration);
});
webBuilder.UseStartup<Startup>();
});

外部库中的扩展方法:

public static void Configure(this LoggerConfiguration loggerConfig,
IServiceProvider provider, IConfiguration config)
{
var sqlserverConnectionString = config["ConnectionStrings:S1"];
var sqlserverLogTable = config["Logging:S2"];
var rollingFileName = config["Logging:S3"];

//SQL CONFIGURATION HERE TO REDUCE THE NUMBER OF LINES...

loggerConfig
.ReadFrom.Configuration(config)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.Enrich.WithAssemblyName()
.Enrich.WithAssemblyVersion()
.WriteTo.File(rollingFileName, restrictedToMinimumLevel: LogEventLevel.Verbose)
.WriteTo.Seq(seqAddress)
.WriteTo.MSSqlServer(
connectionString: sqlserverConnectionString,
sinkOptions: sinkOpts,
columnOptions: columnOpts
);
}

最佳答案

如评论中所述,您当前显示的示例并未显示您将 .UseSerilog() 与主主机构建器一起使用。虽然您可能已经添加到 Web 默认构建器,但基于异常,它没有添加到服务集合中,这是在最终构建主机时解决依赖关系的原因。

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() //<-- THIS WAS MISSING HERE
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseSerilog((provider, context, loggerConfig) => {
loggerConfig.Configure(provider, Configuration);
});
webBuilder.UseStartup<Startup>();
});

其他一切都保持不变。

引用 Serilog.Extensions.Hosting

关于c# - 尝试激活 'Serilog.AspNetCore.RequestLoggingMiddleware' 时无法解析“Serilog.Extensions.Hosting.DiagnosticContext”的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68893597/

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