0
votes

I have application insights connected to my azure functions and use default ILogger for logging.

With default configuration, if you pass an exception in a code such as

log.logError(ex, "Some message")

it will result in both trace entry and an exception entry.

I would like to disable this behavior, so that only unhandled exceptions are captured by AI as an exception entry.

Docs say here (https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger) that to achieve that I need setup the AI like this:

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

however it seems it cannot be done in Azure Functions.

For starters I use built-in ILogger support, so I do not explicitly add AI in Startup.cs but even if I did, in the library there is no such method at all but rather builder.Services.AddApplicationInsightsTelemetry that doesn't have such TrackExceptionsAsExceptionTelemetry property in the options.

I know that a workaround would be to log something like log.logError($"Some message, {ex.Message}, {ex.StackTrace}") but maybe there is some better solution?

1
How do you connect your function to app insights? code-based or code-less?Tiny Wang
code-less, so just by having APPINSIGHTS_INSTRUMENTATIONKEY in the configuration, the AI automatically picks it up and worksGrzegorz Paluch
I just used builder.Services.Configure<ApplicationInsightsLoggerOptions>(o => o.TrackExceptionsAsExceptionTelemetry = false); in my Startup but it didn't work eitherGrzegorz Paluch

1 Answers

0
votes

For my testing, I created the app insights alongside the function, and a default http trigger. I found that the handled exception will logged by trace and there was no exception entry indeed. If I didn't surround with try catch, I got the exception entry. Did I misunderstand in some place?

enter image description here enter image description here enter image description here

==========================Update==========================

If enabled app insights by code, per my testing, I got the same situation as you, like my screenshot below:

enter image description here