Can you log custom properties with Auto-instrumentation?

Emanuel Zhupa 0 Reputation points
2023-03-23T11:32:26.02+00:00

Hi,

I have a ASP.NET MVC application, I have Application Insights enabled with Auto-Instrumentation and I want to add custom properties on the exception for the Application Insight to pickup,

At the moment I have created a Global Exception Handler which extends Exception.Data Dictionary, because I thought Auto-Instrumentation would pick this up, but it doesn't seem that is the case,

Anyone might have an opinion?

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,826 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 13,170 Reputation points
    2023-03-23T20:32:20.2933333+00:00

    To add custom properties to exceptions in Application Insights, you can use Telemetry Initializers. Telemetry Initializers allow you to modify telemetry data before it's sent to Application Insights. You can create a custom Telemetry Initializer to add your custom properties to the exception telemetry.

    Follow these steps:

    1. First, create a custom Telemetry Initializer class that implements ITelemetryInitializer. In this class, override the Initialize method and add your custom properties to the exception telemetry:
    
    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.ApplicationInsights.Extensibility;
    
    public class CustomExceptionTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (telemetry is ExceptionTelemetry exceptionTelemetry)
            {
                // Add your custom properties
                exceptionTelemetry.Properties["CustomProperty1"] = "CustomValue1";
                exceptionTelemetry.Properties["CustomProperty2"] = "CustomValue2";
            }
        }
    }
    
    
    1. Next, update the ApplicationInsights.config file to include your custom Telemetry Initializer:
    <ApplicationInsights>
      ...
      <TelemetryInitializers>
        ...
        <Add Type="YourNamespace.CustomExceptionTelemetryInitializer, YourAssemblyName" />
      </TelemetryInitializers>
    </ApplicationInsights>
    
    

    Replace YourNamespace with the namespace where your CustomExceptionTelemetryInitializer class is located, and replace YourAssemblyName with the name of the assembly.

    1. Now, your custom properties will be added to the exception telemetry in Application Insights. If you need to access the exception data to populate your custom properties, modify your custom Telemetry Initializer class to extract the required data from the ExceptionTelemetry object.

    With this approach, the custom properties will be added to the exceptions captured by Application Insights auto-instrumentation.

    0 comments No comments

  2. AnuragSingh-MSFT 20,431 Reputation points
    2023-03-28T05:54:38.5033333+00:00

    @Emanuel Zhupa , thank you for the question.

    To answer your question - enriching the exception telemetry without using the AppInsights SDK (i.e., through auto-instrumentation) is not possible.

    By default, only the unhandled exceptions from MVC controller methods are tracked automatically (for example, when you enable Application Insights using Auto-Instrumentation method). All exceptions handled by application still need to be tracked manually using the trackException() method.

    Therefore, enriching the exception telemetry without using the AppInsights SDK (i.e., through auto-instrumentation) is not possible. You could use the telemetry processors as suggested by Sedat in his answer or can submit exception manually using the TrackException() method - wherever you are handling it. Please see this link for more details - Exceptions in ApplicationInsights.

    Hope this helps!

    If the answer did not help, please add more context/follow-up question for it, and we will help you out. Else, if the answer helped, please click Accept answer so that it can help others in the community looking for help on similar topics.

    0 comments No comments