Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
erik-kallen opened this issue Oct 23, 2023 · 1 comment

Comments

@erik-kallen
Copy link

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.

@hxhieu
Copy link

hxhieu commented May 1, 2024

+1 on this

Another workaround is not passing the Exception into the logger.Error call and just the error message string. But then we would lose the stack and Sonar scan will complain about exception not passing in etc.

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

No branches or pull requests

2 participants