Azure / Azure/azure-functions-host

Log the exception's message to Application Insights

Open
#7,895 7 comments 0 reactions 0 assignees View on GitHub
needs-investigation
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

I'm using C# Azure Functions.
All exceptions are/shall be logged to our Application Insights. We monitor those logs in the AI by another tool.

I've created a very simple function:
```
[FunctionName(nameof(Text_ExceptionLogging))]
public FunctionResult Text_ExceptionLogging([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestMessage request, ExecutionContext context)
{
var a = SettingsManager.GetValue("-invalid-");
return true;
}
```
`SettingsManager.GetValue` will throw an exception saying this value is not configured through the Azure Portal. When running the function and I'm connected to the output (eg. viewing the local console or the Log stream portal), I can see that message:

> 2021-11-26T00:00:00.110 [Error] Executed 'Text_ExceptionLogging' (Failed, Id=00000000-0000-0000-0000-000000000000, Duration=1ms)Configuration key -invalid- not found [...]

But it's not fully logged to the Application Insights:

column | value
-- | --
timestamp [UTC] | 2021-11-26T00:00:00.311Z
message | Executed 'Text_ExceptionLogging' (Failed, Id=00000000-0000-0000-0000-000000000000, Duration=1ms)
severityLevel | 3
cloud_RoleName | MyApp
operation_Name | Text_ExceptionLogging

If the exception message is not logged, I can't see / filter for it. I've already created a wrapper method some time ago, that catches all exceptions, logs its message and stack trace in separated actions using the built-in logger (which writes to the Application Insights), so I'm able to see additional information. The problem is that this wrapper method was not used here. Sure, I can add this to my `Text_ExceptionLogging` function too, but I think that logging the exception's message would be useful in general for everyone. I can see it in the console, in the log stream, so why wouldn't it be available in AI too?

Contributor guide

Open the contributing guide

Research direction

Reproduce the C# Azure Function with SettingsManager.GetValue("-invalid-") and compare the local console or Log stream output with the Application Insights record. Done means the exception message, including the missing configuration key, is present in Application Insights and can be filtered there.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.