admin管理员组文章数量:1123344
I want to see my ASP.NET Core application's logs in Application Insights.
I'm using the following packages:
Azure.Monitor.OpenTelemetry.AspNetCore
Serilog.AspNetCore
Serilog.Sinks.OpenTelemetry
This is the current setup:
builder.Services.AddOpenTelemetry()
.UseAzureMonitor()
.ConfigureResource(x =>
{
x.AddAttributes(new Dictionary<string, object> { { "service.name", "my-service" } });
})
.WithTracing();
builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console(builder.Environment.IsDevelopment()
? new RenderedCompactJsonFormatter()
: new CompactJsonFormatter())
.WriteTo.OpenTelemetry()
);
The connection string is passed via the environment variable APPLICATIONINSIGHTS_CONNECTION_STRING
. I can see requests in the Application Insights dashboard, but no traces/logs. What's missing here?
I want to see my ASP.NET Core application's logs in Application Insights.
I'm using the following packages:
Azure.Monitor.OpenTelemetry.AspNetCore
Serilog.AspNetCore
Serilog.Sinks.OpenTelemetry
This is the current setup:
builder.Services.AddOpenTelemetry()
.UseAzureMonitor()
.ConfigureResource(x =>
{
x.AddAttributes(new Dictionary<string, object> { { "service.name", "my-service" } });
})
.WithTracing();
builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console(builder.Environment.IsDevelopment()
? new RenderedCompactJsonFormatter()
: new CompactJsonFormatter())
.WriteTo.OpenTelemetry()
);
The connection string is passed via the environment variable APPLICATIONINSIGHTS_CONNECTION_STRING
. I can see requests in the Application Insights dashboard, but no traces/logs. What's missing here?
- Please share your configuration file. – Harshitha Commented 8 hours ago
1 Answer
Reset to default 1UseAzureMonitor
calls this under the hood:
builder.Services.AddLogging(
{
logging.AddOpenTelemetry(builderOptions =>
{
builderOptions.IncludeFormattedMessage = true;
});
});
which in turn registers an <ILoggerProvider, OpenTelemetryLoggerProvider>
.
AddSerilog
has a default option of writeToProviders = false
. If you instead toggle it to true, logs will push through to Application Insights, but it tells you an equivalent Serilog sink should be used instead of writing to all providers.
After toggling that flag, you get double the logs, but I solved that with builder.Logging.ClearProviders();
.
I don't know how the library authors intended this integration to look like, but this is my currently working setup:
builder.Logging.ClearProviders();
builder.Services.AddOpenTelemetry()
.UseAzureMonitor()
.ConfigureResource(x =>
{
x.AddAttributes(new Dictionary<string, object> { { "service.name", "my-service" } });
});
builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console(builder.Environment.IsDevelopment()
? new RenderedCompactJsonFormatter()
: new CompactJsonFormatter()), writeToProviders: true);
本文标签: How to send logs to Application Insights with Serilog and Azure Monitor OpenTelemetryStack Overflow
版权声明:本文标题:How to send logs to Application Insights with Serilog and Azure Monitor OpenTelemetry? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736564227a1944681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论