Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights when in debug mode

I've just enabled Application Insights in my MVC application and noticed that when debugging locally, that the trace information is being captured within my Azure Application Insight.

When in debug mode, I want to stop my app from recording the events within my Azure Application Insight but still display the Events and logging information in the Diagnostic Tools > Events window within Visual Studio.

I've tried the following and whilst this stop the events from being captured in my Azure AI, Visual Studio no longer displays the debug information in Events window.

 protected void Application_Start()
 {
        #if DEBUG
        TelemetryConfiguration.Active.DisableTelemetry = true;
        #endif
 }

I've surfed the net for an answer to no avail. Hope someone can help.

like image 985
jgill09 Avatar asked Aug 15 '16 16:08

jgill09


People also ask

How do I view debug logs in app Insights?

Go to Application Insights resource in your resource group. Go to Logs under Monitoring section. Click on traces eye button to get log traces.

How do I see exceptions in application Insights?

Open the Application Insights Search telemetry window in Visual Studio. While debugging, select the Application Insights dropdown box. Select an exception report to show its stack trace. To open the relevant code file, select a line reference in the stack trace.

How long does app Insights keep logs?

How long is the data kept? Raw data points (that is, items that you can query in Analytics and inspect in Search) are kept for up to 730 days. You can select a retention duration of 30, 60, 90, 120, 180, 270, 365, 550, or 730 days.

How do I monitor my Azure application Insights?

The easiest way to get started consuming Application insights is through the Azure portal and the built-in visual experiences. Advanced users can query the underlying data directly to build custom visualizations through Azure Monitor Dashboards and Workbooks.


1 Answers

The cheapest way to do this is to set your Instrumentation Key to all 0's. There is no NULL iKey so it effectively will just drop the message.

00000000-0000-0000-0000-000000000000

If you want to use Application_Start() you can do this using either the #DEBUG directive or you can use System.Diagnostics.Debugger.IsAttached property. However this method is not completely reliable. You can try but your experience may not be consistent.

If you have the time you should make a TelemetryInitializer that would change the Instrumentation Key based on if the debugger is attached or not. This will make sure this only happens if you're inside a debugging session. That way if you accidentally release Debug to production you won't lose your telemetry.

public class CustomeWebRequestTelemetryModule :  Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry != null && System.Diagnostics.Debugger.IsAttached)
        {
            telemetry.Context.InstrumentationKey = "00000000-0000-0000-0000-000000000000";
        }
    }
}
like image 107
James Davis - MSFT Avatar answered Sep 27 '22 18:09

James Davis - MSFT