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 |1 Answer
Reset to default 1You 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:
if
block ? – Harshitha Commented Feb 17 at 9:00