[英]CorrelationId is Null when using serilog enricher nuget package
我想將 correlationId 的值填充到我的 applicationInsights。 現在該值為空。 我已經安裝了 serilog.enrichers.correlationId nuget 包並在我的 startup.cs 文件中有 services.AddHTTPContextAccessor,但 correlationId 的值仍然為空。 我錯過了什么嗎?有什么建議嗎? 我的來源是:- https://github.com/ekmsystems/serilog-enrichers-correlation-id
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
以上是我在 startup.cs 中的記錄器配置。 我在 appsettings.json 的 Enrich 標簽中定義的所有其他增強器都被填充,但相關 ID 為空。
這是我的測試結果:
我在下面做了什么:
在我的 asp.net 核心應用中,先安裝相關包,然后編輯Program.cs
-> CreateHostBuilder
:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration).Enrich.WithCorrelationId()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.ApplicationInsights(new TelemetryConfiguration { InstrumentationKey = "your_instrumentation_key" }, TelemetryConverter.Traces)
);
我的包裹:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
</ItemGroup>
</Project>
你尊重這個嗎? 您需要調用services.AddHttpContextAccessor();
https://github.com/ekmsystems/serilog-enrichers-correlation-id#:~:text=You%20need%20to%20register%20the%20IHttpContextAccessor%20singleton%20so%20that%20the%20enricher%20has%20access%20to %20the%20requests%20HttpContext%20so%20that%20it%20can%20attach%20the%20correlation%20ID%20to%20the%20request/response
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.