Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Duplicate exceptions from Ilogger + RequestTrackingModule #861

Closed
cijothomas opened this issue Apr 6, 2019 · 6 comments
Closed

Duplicate exceptions from Ilogger + RequestTrackingModule #861

cijothomas opened this issue Apr 6, 2019 · 6 comments
Labels
Milestone

Comments

@cijothomas
Copy link
Contributor

When an exception is thrown from Controller, RequestTrackingModule reports it.
https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs#L480

Since ILogger is also enabled by default now, it also captures this exception.

Repro Steps

  1. Use any WebAPI app, and throw unhandled exception from controller.

Actual Behavior

2 exception telemetry, 1 from Ilogger, 1 from request tracking module

Expected Behavior

1

Version Info

SDK Version :
.NET Version :
How Application was onboarded with SDK(VisualStudio/StatusMonitor/Azure Extension) :
OS :
Hosting Info (IIS/Azure WebApps/ etc) :

@cijothomas cijothomas added this to the 2.7.0 milestone Apr 6, 2019
@cijothomas
Copy link
Contributor Author

workaround

public void ConfigureServices(IServiceCollection services)
        {            
            services.ConfigureTelemetryModule<Microsoft.ApplicationInsights.AspNetCore.RequestTrackingTelemetryModule>((req, o) => req.CollectionOptions.TrackExceptions = false);
        }

@federicobarera
Copy link

federicobarera commented Apr 18, 2019

as per this: https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/268 please also keep in mind that other middlewares (without much config options) are possibly logging exceptions.

EDIT:

Doesn't seem the RequestTrackingModule being the issue

EDIT 2:

Where is this done and how to disable this behavior?
https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions

Starting with Application Insights Web SDK version 2.6 (beta3 and later), Application Insights collects unhandled exceptions thrown in the MVC 5+ controllers methods automatically. If you have previously added a custom handler to track such exceptions (as described in following examples), you may remove it to prevent double tracking of exceptions.

@alex-zyl
Copy link

Having similar issue when using Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.
ExceptionHandlerMiddleware itself logs unhandled exception and also writes "Microsoft.AspNetCore.Diagnostics.HandledException" event into diagnostic source which in turn triggers HostingDiagnosticListener to process the event.

@federicobarera
Copy link

@alex-zyl following the other thread, seems I have resolved with

  1. Remove UseApplicationInsights() from WebHost.CreateDefaultBuilder(args)
  2. In Startup.cs add:
services.AddApplicationInsightsTelemetry();           services.ConfigureTelemetryModule<Microsoft.ApplicationInsights.AspNetCore.RequestTrackingTelemetryModule>((req, o) => req.CollectionOptions.TrackExceptions = false);
  1. In ConfigureLogging:
c.AddApplicationInsights(h.Configuration["ApplicationInsights:InstrumentationKey"]);
  1. Not being in DeveloperMode (I don't know if impacts or not)

I know they worked on adding logging directly into the main app insights package, but we are not using betas in prod so, our config is

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.6.1" />
 <PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.9.1" />

@cijothomas
Copy link
Contributor Author

@alex-zyl If you are using ExceptionHandlerMiddleware, you are right - you'll get duplicate logs. To prevent ai from getting it, apply a filter as shown below.
{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware": "None"
}
},
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

@cijothomas
Copy link
Contributor Author

Closed with #863

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants