Application Insights telemetry processor not catching exception

1.2k views Asked by At

I have a custom Telemetry Processor that sends custom exception fields to Application Insights.

For example if I throw a custom exception like this one

class MyException : Exception
{
   public string AdditionalData { get; set; }
}

then it sends AdditionalData as a custom app insights field.

public class CustomExceptionDataTelemetryProcessor : ITelemetryProcessor
{
    /// <summary>
    /// Default exception fields that should be ignore because they are part of logs by default
    /// </summary>
    private static IEnumerable<string> _DefaultExceptionFields = new Exception().GetType().GetProperties().Select(e => e.Name);
    private readonly ITelemetryProcessor _next;

    public CustomExceptionDataTelemetryProcessor(ITelemetryProcessor next)
    {
        _next = next;
    }

    public void Process(ITelemetry item)
    {
        ModifyItem(item);
        _next.Process(item);
    }

    /// <summary>
    /// Adds exception fields as custom data
    /// </summary>
    private void ModifyItem(ITelemetry item)
    {
        if (!(item is ExceptionTelemetry exceptionTelementry))
        {
            return;
        }

        var exception = exceptionTelementry.Exception;
        var exceptionType = exception.GetType();

        foreach (var property in exceptionType.GetProperties())
        {
            // ignore default fields
            if (_DefaultExceptionFields.Contains(property.Name))
            {
                continue;
            }
            
            var value = property.GetValue(exception);
            var serializedValue = (value is string) ? value.ToString() : HttpClientUtils.Serialize(value);
            exceptionTelementry.Properties[property.Name] = serializedValue;
        }
    }
}

I register this processor like this

var telemetryBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
telemetryBuilder.Use((next) => new CustomExceptionDataTelemetryProcessor(next));
telemetryBuilder.Build();

Everything works fine with Microsoft.ApplicationInsights.AspNetCore version 2.2.1.

If I upgrade to latest version 2.15.0 it still works fine, but I get a warning that the registration code is deprecated so I changed it per the documentation

services.AddApplicationInsightsTelemetryProcessor<CustomExceptionDataTelemetryProcessor>();

But now the exception doesn't get 'caught' anymore by the processor. Only items of type RequestTelemetry or DependencyTelemetry. No items of type ExceptionTelemetry.

I could leave the registration with the old way, but thinking that it might be a sign that something is not quite right and might cause other issues elsewhere.

I tried to remove all initializers and processors before registering my own, but it doesn't help. Something else might be filtering out the exception.

services.RemoveAll(typeof(ITelemetryInitializer));
services.RemoveAll(typeof(ITelemetryProcessor));
1

There are 1 answers

7
SzilardD On BEST ANSWER

It works with version 2.13.1 of Microsoft.ApplicationInsights.AspNetCore

I also had Microsoft.ApplicationInsights NuGet package installed with a different version. Removed it and kept only Microsoft.ApplicationInsights.AspNetCore.