简体   繁体   中英

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.

过去 24 小时内的 Azure 门户 5xx 错误

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?

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 .

在此处输入图像描述

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

在此处输入图像描述

Check the error count in Failures .

在此处输入图像描述

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.

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM