I have a .NET 8 web API hosted in Azure App Service.
I encountered an issue where I'd get a 500 response due to an error with my DB logic, but there were no entries in the exception log in the App Insights section of the portal.
I added a few test logging lines to my controller code to explicitly catch the exception and try manually logging, and found that nothing was logged if I passed an exception as the first argument to the Log* methods. If I don't pass the exception, and just pass a message, it DOES log.
I'm using the Microsoft.ApplicationInsights.AspNetCore package on the latest version (2.22.0).
My config:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
In my Startup file I'm calling services.AddApplicationInsightsTelemetry();
My controller code to test this out is:
try
{
var updatedCase = await ...; // DB update logic that may throw ConcurrencyException
return Ok(...);
}
catch (ConcurrencyException cEx)
{
logger.LogError(cEx, "DEBUG ERROR: CAUGHT CONCURRENCY EXCEPTION"); // NOT logged
logger.LogInformation(cEx, "DEBUG INFO: CAUGHT CONCURRENCY EXCEPTION"); // NOT logged
logger.LogInformation("DEBUG INFO: CAUGHT CONCURRENCY EXCEPTION (NO EX)"); // LOGGED under traces in App Insights
throw; // The rethrown exception is NOT logged
}
I'm not sure if it helps but the custom ConcurrencyException looks like this - Do I need to be doing something else with this to make it loggable?:
public class ConcurrencyException : Exception
{
public ConcurrencyException() { }
public ConcurrencyException(string message)
: base(message) { }
public ConcurrencyException(string message, Exception inner)
: base(message, inner) { }
}
And the library code that throws it looks like this:
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.Conflict || ex.Headers["x-ms-substatus"] == "409" || ex.SubStatusCode == 409)
{
throw new ConcurrencyException($"Concurrency conflict when appending to stream {streamId}. Expected revision {firstEventNumber - 1}", ex);
}
If I don't catch/handle the exceptions, I get a 500 response, but nothing is logged in AppInsights.
Weirdly, if I catch the ConcurrencyException and attempt to log it, it does not appear in AppInsights. If I catch it and throw a new Exception, then that new exception DOES get logged, e.g.
try
{
...
}
catch (ConcurrencyException cEx)
{
...
throw new Exception("DEBUG: A NEW EXCEPTION"); // Note that if I do this, then it DOES get logged!
}
How can I get App Insights to log these exceptions?
I am able to log Error as an Exception with and without passing EX.
Exception=>End-to-endtransaction detailsYou can see the Exception with Severity Level.
Error with EX:
Information with EX:
My
Program.csfile:appsettings.json