使用opentelemetry-dotnet在NewRelic中无法查看请求关联日志
问题:ASP.NET请求处理日志无法在New Relic显示
ASP.NET应用启动时的部分日志能正常出现在New Relic中,但处理HTTP请求时通过ILogger<T>输出的日志始终无法显示。已配置OTEL_DIAGNOSTICS.json但未记录任何错误;ConsoleExporter运行正常,追踪数据可正常查看,New Relic的NrIntegrationError集合中也无内容。怀疑New Relic仅展示未关联追踪和跨度的日志,配置代码如下:
builder.Services.AddOpenTelemetry() .ConfigureResource(builder => builder.AddService(Otel.ServiceName, serviceVersion: Otel.ServiceVersion)) .WithLogging(logging => logging .AddConsoleExporter() .AddOtlpExporter(otlp => { otlp.Endpoint = new Uri($"{_otelEndpoint}/v1/logs"); otlp.Headers = $"api-key={_otelApiKey}"; otlp.Protocol = _otelProtocol; // Using Http/Protobuf }), options => { options.IncludeFormattedMessage = true; options.IncludeScopes = true; options.ParseStateValues = true; }) .WithTracing(tracing => tracing .AddSource(Otel.ServiceName) .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddRedisInstrumentation() .AddAWSInstrumentation() .AddOtlpExporter(options => { options.Endpoint = new Uri($"{_otelEndpoint}/v1/traces"); options.Headers = $"api-key={_otelApiKey}"; options.Protocol = _otelProtocol; })) .WithMetrics(metrics => metrics .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddOtlpExporter(options => { options.Endpoint = new Uri($"{_otelEndpoint}/v1/metrics"); options.Headers = $"api-key={_otelApiKey}"; options.Protocol = _otelProtocol; }));
排查与解决建议
确认日志与追踪跨度的关联
确保ILogger记录日志时能获取到当前请求的追踪上下文。检查AddAspNetCoreInstrumentation()配置是否完整,未过滤请求跨度;同时验证控制器、服务中使用ILogger<T>时,是否处于请求的跨度范围内。调整日志级别过滤规则
显式配置OTel日志的最低级别,避免请求处理时的日志(如Information、Debug)被默认过滤:.WithLogging(logging => logging // 保留原有配置 .SetMinimumLevel(LogLevel.Information) // 根据实际需求调整级别 )修改New Relic日志查询条件
New Relic可能默认未展示关联追踪的日志,尝试在日志查询中添加过滤条件:has trace.id,查看是否能找到请求处理时的日志。启用OTel详细诊断日志
修改OTEL_DIAGNOSTICS.json,将OTel相关日志级别设为Debug,排查是否存在隐藏的导出错误:{ "Logging": { "LogLevel": { "OpenTelemetry": "Debug", "OpenTelemetry.Exporter.OpenTelemetryProtocol": "Debug" } } }
内容的提问来源于stack exchange,提问作者Jackson Mourão
相关产品推荐
相关产品推荐

