我正在尝试跟踪 Application Insights 中的请求,如此屏幕截图所示:
但只有当我在开发中提出请求(使用 localhost)时,才会显示请求。在生产中,日志仅显示为跟踪:
我怎样才能让它在生产中也能工作?
我使用以下代码将 Serilog 用作接收器:
// Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hostBuilderContext, services, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostBuilderContext.Configuration);
loggerConfiguration.AddApplicationInsightsLogging(services, hostBuilderContext.Configuration);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
// Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
在services.AddApplicationInsightsTelemetry()
Startup.ConfigureServices 中。
# appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning"
}
},
"Serilog": {
"Using": [
"Serilog.Sinks.ApplicationInsights"
],
"MinimumLevel": "Information",
"Override": {
"Default": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft.AspNetCore": "Warning"
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console"
},
{
"Name": "ApplicationInsights",
"Args": {
"restrictedToMinimumLevel": "Information",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
}
}
],
"Enrich": [
"FromLogContext",
"WithSpan"
],
"Filter": [
]
},
"AllowedHosts": "*"
}
# appsettings.Development.json
{
"APPINSIGHTS_INSTRUMENTATIONKEY": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
}