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.

Developer technologies | ASP.NET | ASP.NET API
{count} votes

2 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 5,410 Reputation points Microsoft External Staff Moderator
    2025-08-13T09:29:51.7633333+00:00

    Hi,

    In case you want to keep Microsoft.ApplicationInsights.Profiler.AspNetCore for the profiler, you can stop it from sending logs/traces so that OpenTelemetry is the only one doing that.

    Right now, your duplication happens because that package drags in the full Application Insights SDK, which automatically registers its own ILoggerProvider and telemetry modules.

    You can try the following:

    1. Disable the AI Logging Provider

    Add this right after services.AddServiceProfiler(); to remove the logging provider the SDK registers:

    services.Configure<TelemetryConfiguration>(config =>
    {
        // Remove the Application Insights logging provider
        config.DefaultTelemetrySink.TelemetryProcessorChainBuilder = null;
    });
     
    services.AddLogging(loggingBuilder =>
    {
        // Remove AI provider from the logging pipeline
        loggingBuilder.ClearProviders(); // optional if you only want OTel
        loggingBuilder.AddOpenTelemetry();
    });
    

    This ensures the AI SDK doesn't push logs through its pipeline.


    1. Disable AI Telemetry Modules You Don’t Want

    You can disable everything except the profiler:

    services.Configure<TelemetryConfiguration>((config) =>
    {
        // Keep profiler, remove everything else
        foreach (var module in config.TelemetryModules
            .Where(m => !(m is Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule))
            .ToList())
        {
            config.TelemetryModules.Remove(module);
        }
     
        // Remove all processors except profiler ones
        foreach (var processor in config.DefaultTelemetrySink.TelemetryProcessors
            .Where(p => p.GetType().Name != "ServiceProfilerTelemetryProcessor")
            .ToList())
        {
            config.DefaultTelemetrySink.TelemetryProcessors.Remove(processor);
        }
    });
    

    This effectively neuters AI’s auto-collection while leaving the profiler pipeline intact.


    1. Filtering Logs in appsettings.json

    Once AI’s own logging provider is gone, your log filtering applies only to OpenTelemetry:

    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning",
        "Microsoft": "Warning"
      }
    }
    

    Hope this works out for you.

    1 person found this answer helpful.

  2. 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.