Reputation: 846
I'm having some trouble with Application Insights when running a C# v3 Function App. Everything is setup within Azure to use Application insights, as per the instructions. Within the host.json file, I've included the following settings:
"logging": {
"LogLevel": {
"Default": "Information"
},
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
I am using Dependency Injection, so have a startup class within the app, and then inject ILogger class wherever required:
using Microsoft.Extensions.Logging;
...
builder.Services.AddLogging();
There are a few occasions within code where I'm catching an exception, handling it, and want to log it rather than throw an exception. E.g. looking for a blob container image that may or may not exist - I can still return a valid response rather than throwing the whole thing because of a single, trivial issue.
However, Application Insights does not seem to be logging the exception within the trace, even though it has been passed to the logger:
catch (ArgumentException e)
{
var msg = $"Error encountered blah blah blah";
logger.LogWarning(e, msg);
return Result.Failure<string>(msg);
}
I can see within Application Insights the warning being logged, along with the message, but the stack trace is always missing from the message/custom dimensions within the trace. The same happens if I use LogError.
Custo:
traces
| where cloud_RoleName == "my-app" and severityLevel == 2
I've looked through a fair amount of documentation and questions on here, but struggling to find an answer that works, or that explains well what is going on. It seems very much with Azure Functions + App Insights that either "it just magically works if you turn it on", or the answers are for ASP.NET core, or just plain out of date.
Is the problem that only exceptions thrown by the app will show up in the Exceptions area of App Insights with a stack trace? (if so, can you log errors here without throwing an exception?)
Am I missing a key bit of config in host.json or startup? A library? (I've already tried adding Microsoft.Azure.WebJobs.Logging.ApplicationInsights)
I'm also looking at changing over to use Serilog + an App Insights sink if necessary - but don't especially want to do this when it seems like it should "just work out of the box".
Can anyone help?
Upvotes: 4
Views: 5509
Reputation: 846
Thanks to @PeterBons for his help, in order to get your logged exceptions to appear in the Exceptions table in App Insights you need to make sure that:
If you're using sampling, you add "Exception" to the "samplingExcludedTypes" in your host.json to make sure they all get logged
You need to reference the "Microsoft.Azure.WebJobs.Logging.ApplicationInsights" nuget package in your project (I left everything else the same and removed this - boom, no logging to exceptions)
See Microsoft Documentation for more details about the host file.
For example, this would enable Sampling, but not for requests or exceptions.
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond": 20,
"excludedTypes": "Request;Exception"
}
}
}
}
Upvotes: 4