Search code examples
azureazure-web-app-serviceasp.net-core-webapiazure-application-insights

Application Insights not showing all Errors


Last night, we received hundreds of complaints of errors from one of our Azure Web Apps.

In the Azure Portal, we looked at the Web App & could see we were getting thousands of 5xx Errors.

Azure Portal 5xx Errors Over Past 24hrs

The app is a .NET 6 Web API & is configured to use the Application Insights SDK. However, Application Insights only shows 44 errors during this same timeframe. It shows thousands of successful requests during this timeframe as well. So, we know Application Insights was connected & working as there were thousands of logs there, but we would expect to see many more Errors Logs.

This is our appsettings.json:

{
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "Instrumentation Key from Azure"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Error"
      }
    }
  } 
}

We have this in our Pragram.cs:

var services = builder.Services;
var configuration = builder.Configuration;

services.AddApplicationInsightsTelemetry(configuration["ApplicationInsights:InstrumentationKey"]);

And in our Controllers:

namespace MyAPI.Controllers
{
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class MyController : ControllerBase
    {
        private readonly ILogger _logger;
        
        public MyController(ILogger<MyController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public async Task<ActionResult> MyTask()
        {
            try
            {
                // Do things async

                return Ok()
            }
            catch (Exception e)
            {
                _logger.LogError("{error}", e.Message);
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }
    }
}

What can we look at to get visibility into these errors?


Solution

  • Iam able to log the error with status code 500 with the same configuration from your appsettings.json file.

    To log the 500 error, I have explicitly thrown an exception.

    My Controller:

    [HttpGet(Name = "Task")]
    public ActionResult MyTask()
    {
        try
        {
            throw new Exception();       
        }
        catch (Exception e)
        {
            _logger.LogError("{error}", e.Message);
            return StatusCode(StatusCodes.Status500InternalServerError, e.InnerException);
        }
    }
    

    You can see that 500 errors are logged as Request. Check the same in the Transaction Search.

    enter image description here

    • Click on the REQUEST entry to get the detailed list of the error.

    enter image description here

    Check the error count in Failures.

    enter image description here

    It appears that many of these errors are occurring due to a SQL timeout while querying a 3rd party database.

    Refer the SOThread which explains logging the SQL timeout errors.

    Iam able to log the SQL Time out Exceptions with the below code.

    public ActionResult MyTask()
     {           
         SqlConnection conn = new SqlConnection("Data Source=****\\****;Initial Catalog=Test;Integrated Security=True");
         TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
         TelemetryClient telemetry = new TelemetryClient(configuration);
         try
         {
             conn.Open();
             SqlCommand cmd= new SqlCommand();
             cmd.CommandText= "Select  * from Test";
             cmd.ExecuteNonQuery();
             return Ok();
         }
         catch (Exception e)
         {
             telemetry.TrackException(e);
             _logger.LogError("{error}", e.StackTrace);
             return StatusCode(StatusCodes.Status500InternalServerError, e.InnerException);
         }
     }
    
    • SQL Timeout errors are logged as Traces.

    enter image description here

    enter image description here