Make Azure include exception message in logs

  Kiến thức lập trình

All my code in my aspcore app includes text in the exception, like:

throw new StrangeException(500, "Could not find user");

However in Azure portal in App Insights, the message text is getting stripped. I see plenty of info on an exception but not the text, the only thing I need:

I have set ASPNETCORE_DETAILEDERRORS to true in my environment variables to no avail. Is there a way to prevent it from stripping the error text?

2

  • I am able to log Exception with throw new Exception(ex.Message);

and

throw new Exception("Could not find user");

_logger.LogInformation("Controller Information");
_logger.LogError("Error message");
try
{
     throw new Exception();
}
catch (Exception ex)
{
    //throw new Exception(ex.Message);
    throw new Exception("Could not find user");
}

My Program.cs file:

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

My appsettings.json file:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=****;IngestionEndpoint=https://eastus2-3.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/;ApplicationId=****"
  }
}
  • AFAIK, loggingBuilder.AddConsole(); is not required to log traces to ApplicationInsights. It is used to send Console logs to LogStream.
Recognized by Microsoft Azure Collective

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT