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?

1

There are 1 best solutions below

0
Harshitha On

I am able to log Error as an Exception with and without passing EX.

 catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.Conflict || ex.Headers["x-ms-substatus"] == "409" || ex.SubStatusCode == 409)
 {
   _logger.LogError("DEBUG: A NEW EXCEPTION without Ex");
   _logger.LogError(ex, "DEBUG ERROR: Error EXCEPTION with ex");
   _logger.LogInformation(ex, "DEBUG INFO: Information EXCEPTION with ex"); 
   _logger.LogInformation("DEBUG INFO: CAUGHT CONCURRENCY EXCEPTION (NO EX)"); 
 }
  • In Transaction Search you can only see the EX messages.

enter image description here

  • Click on the Exception => End-to-end transaction details

You can see the Exception with Severity Level.

Error with EX:

enter image description here

Information with EX: enter image description here

  • Also set the LogLevel as shown below.

My Program.cs file:

builder.Services.AddApplicationInsightsTelemetry(new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
   ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]
});
builder.Logging.SetMinimumLevel(LogLevel.Information);

appsettings.json

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
       "ConnectionString": "InstrumentationKey=****;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/"

  }
}