Skip to content

Make it possible to log events as traces even if they contain exception data #216

@erik-kallen

Description

@erik-kallen

Describe your suggestion

Currently, when using the TraceTelemetryConverter, if a log message contains an exception it is logged to AI as an ExceptionTelemetry, otherwise as a TraceTelemetry. At least in our scenario, we don't really differentiate that much between exceptions and other errors, they must all be handled adequately. The current approach means that all our queries must start with exceptions | union traces, and must also account for the data in the tables being different (for example the message in exceptions is the exception message, not the logged message).

When an exception is logged as a trace, it should have customData["exception"] = exception.ToString() (or something)

It would probably also be useful for someone to have the options LogExceptionsAs = "Exceptions" (default) | "Traces" | "Both"

Application Insights itself seems to allow this with

builder.AddApplicationInsights(options => options.TrackExceptionsAsExceptionTelemetry = false);

Describe alternatives you've considered

My current workaround is to write my own TelemetryConverter

public class ExceptionsAsTracesTelemetryConverter : TraceTelemetryConverter
{
    public override IEnumerable<ITelemetry> Convert(LogEvent logEvent, IFormatProvider formatProvider)
    {
        if (logEvent.Exception != null)
        {
            var evtCopy = new LogEvent(logEvent.Timestamp, logEvent.Level, null, logEvent.MessageTemplate, logEvent.Properties.Select(kvp => new LogEventProperty(kvp.Key, kvp.Value)));
            foreach (TraceTelemetry t in base.Convert(evtCopy, formatProvider))
            {
                t.Properties["exception"] = logEvent.Exception.ToString();
                yield return t;
            }
        }
        else
        {
            foreach (var e in base.Convert(logEvent, formatProvider))
            {
                yield return e;
            }
        }
    }
}

This works, but it seems to me that this is a feature that would make sense to have built-in.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions