OpenTelemetry with App Insights Profiler

Ruslan Nigmatullin 0 Reputation points
2025-04-07T11:40:08.22+00:00

Hello,

We migrated our project to OpenTelemetery from application Insight.

Of course, AppInsight is our Exporter now.

However, I don't want to lose the "Service Profiler". So I installed ONLY "Microsoft.ApplicationInsights.Profiler.AspNetCore".

For that, I just added the "services.AddServiceProfiler();" before the OpenTelemetryBuilder is initialized.

Code looks like this:

 public static IServiceCollection AddOneHubLoggingAndTelemetry(this IServiceCollection services, IConfiguration configuration) 
{
	var appInsight = configuration.GetApplicationInsightsOptions();
	services.AddLogging(loggerBuilder =>
		{
		    loggerBuilder
		        .Configure(c => c.ActivityTrackingOptions = ActivityTrackingOptions.TraceId |
		                                                    ActivityTrackingOptions.SpanId |
		                                                    ActivityTrackingOptions.TraceFlags |
		                                                    ActivityTrackingOptions.Tags |
		                                                    ActivityTrackingOptions.ParentId);
		    loggerBuilder.AddOpenTelemetry(opt =>
		    {
		        opt.IncludeScopes = true;
		        opt.IncludeFormattedMessage = true;
		        opt.ParseStateValues = true;
		    });
		});
	
	// Initializae Profiler
	services.AddSingleton<ITelemetryInitializer, AppInsightCloudRoleTelemetryInitializer>();
	services.AddServiceProfiler();

	services.AddOpenTelemetry()
	.UseAzureMonitor(c =>
            {
                c.ConnectionString = appInsight.ConnectionString;
                c.SamplingRatio = appInsight.SamplingPercent / 100.0f;
            })
	.ConfigureResource(r =>
    	r.AddService(appInsight.ServiceName, serviceVersion: configuration.GetVersion(), serviceInstanceId: Environment.MachineName))
 	.WithMetrics(m => {
		m.AddHttpClientInstrumentation()
			.AddRuntimeInstrumentation()
			.AddAspNetCoreInstrumentation();
		})
	.WithTracing(t => {
	    t.AddAspNetCoreInstrumentation()
	        .AddHttpClientInstrumentation();
    
	})
	.WithLogging(l => ...);

	return service;    
}

Now I have both, BUT, looks like AppInsight adding logs and duplicate the one that OpenTelemetry is sending.

Moreover, I am not able to filtrate the logs with "appsettings.json":

"Logging": {
  "LogLevel": {
    "Default": "None",
    "Microsoft.AspNetCore": "Warning",
    "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information",
    "Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter": "Warning", 
  },
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "None"
    }
  }
},

It simply doesn't work anymore.

How can I get rid of traces/logs of ApplicationInsight and use it only for Profiling, and how to filter the OpenTelemetry logs?

Thank you.

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
414 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ruslan Nigmatullin 0 Reputation points
    2025-04-09T12:29:42.5733333+00:00

    Okay, looks like the answer - you can't.

    In the end, I simply removed the "Microsoft.ApplicationInsights.Profiler.AspNetCore".

    And waiting for the "https://github.com/Azure/azuremonitor-opentelemetry-profiler-net" to be out of pre-release.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.