最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How can I make the Azure Monitor OTEL package not fail on missing Application Insights connection string, but silently cont

programmeradmin2浏览0评论

I'm configuring an ASP.NET Core application with Azure Monitor OpenTelemetry to send logs to Application Insights.

Before using the new Azure Monitor OTEL package Azure.Monitor.OpenTelemetry.AspNetCore, I used the Application Insights package Microsoft.ApplicationInsights.AspNetCore, which was enabled with:

builder.Services.AddApplicationInsightsTelemetry();

Here, if the Application Insights connection string wasn't available, it would silently continue.

However, with the new package, if the connection string is not available, I get an error:

builder.Services.AddOpenTelemetry().UseAzureMonitor();

Error: System.InvalidOperationException: A connection string was not found. Please set your connection string.

How can I use the new package, so that it doesn't fail on a missing connection string, but silently continues?

I'm configuring an ASP.NET Core application with Azure Monitor OpenTelemetry to send logs to Application Insights.

Before using the new Azure Monitor OTEL package Azure.Monitor.OpenTelemetry.AspNetCore, I used the Application Insights package Microsoft.ApplicationInsights.AspNetCore, which was enabled with:

builder.Services.AddApplicationInsightsTelemetry();

Here, if the Application Insights connection string wasn't available, it would silently continue.

However, with the new package, if the connection string is not available, I get an error:

builder.Services.AddOpenTelemetry().UseAzureMonitor();

Error: System.InvalidOperationException: A connection string was not found. Please set your connection string.

How can I use the new package, so that it doesn't fail on a missing connection string, but silently continues?

https://learn.microsoft/en-us/azure/azure-monitor/app/opentelemetry-configuration?tabs=aspnetcore

Share Improve this question asked Feb 15 at 15:49 ShuzhengShuzheng 14k29 gold badges116 silver badges227 bronze badges 2
  • Did you try using the if block ? – Harshitha Commented Feb 17 at 9:00
  • Provided an answer below @shuzheng – RithwikBojja Commented Feb 18 at 8:52
Add a comment  | 

1 Answer 1

Reset to default 1

You can simply avoid that getting that error by testing if the connection string exists or not as below:

using Azure.Monitor.OpenTelemetry.AspNetCore;
using OpenTelemetry;

var cho_buldr = WebApplication.CreateBuilder(args);
var testcon = cho_buldr.Configuration["test"];
var rithbuidler = cho_buldr.Services.AddOpenTelemetry();
if (!string.IsNullOrEmpty(testcon))
{
    rithbuidler.UseAzureMonitor();
}
cho_buldr.Services.AddControllers();
----
----
var app = cho_buldr.Build();
----
----
----
app.Run();

Output:

Works without giving any exception:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论