microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
565 stars 287 forks source link

Application Insights is not capturing simple exception telemetry when using ILogger.Log(LogLevel, Exception, string) #2236

Closed stevieray8450 closed 3 years ago

stevieray8450 commented 3 years ago

Describe the bug

When invoking ILogger.Log as mentioned here and here, no exception telemetry is getting captured or recorded. I can observe this from Visual Studio 2019 as well as inside the Azure Application Insights instance accessed via the Azure Portal.

Other telemetry -- dependencies, metrics, and requests, for example -- get recorded as expected, and are accessible via Visual Studio 2019 and Azure Portal.

To Reproduce

ApplicationInsights:InstrumentationKey and Logging:ApplicationInsights:LogLevel:Default are coming from Azure App Configuration and are configured with the correct instrumentation key (confirmed by the fact that we have other expected telemetry getting recorded) and "Information" value, respectively.

Repro code. Simple API controller:

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using TestProject.Enum;

namespace TestProject.Web.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class PipelineController : ControllerBase
    {
        private readonly ILogger<PipelineController> simpleLogger;

        public PipelineController(ILogger<PipelineController> simpleLogger)
        {
            this.simpleLogger = simpleLogger;
        }

        /// <summary>
        /// Queues a pipeline extraction
        /// </summary>
        [HttpPost]
        [Route("Queue")]
        public IActionResult Queue(Payload payload, Profile profile)
        {
            simpleLogger.Log(LogLevel.Error, new ArgumentException(), "just a test");

            try
            {
                throw new ArgumentException();
            }
            catch (ArgumentException e)
            {
                simpleLogger.Log(LogLevel.Error, e, "just a test");
                return BadRequest(e.Message);
            }
        }
    }
}

Demonstrating registration at Startup:

using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using Web.Model.Validation;

namespace Web
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration) => Configuration = configuration;

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAzureAppConfiguration();

            services.AddControllers().ConfigureApiBehaviorOptions(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    return InvalidRequestResponse.GetBadRequestObjectResult(context);
                };
            });

            services.AddOptions();
            services.AddFeatureManagement();
            services.AddAuthentication(IISDefaults.AuthenticationScheme);

            services.AddApplicationInsightsTelemetry();
        }
    }
}

Result: neither call to ILogger.Log results in exception telemetry getting recorded

We will close this issue if:

For Immediate Support

For immediate support relating to the Application Insights .NET SDK we encourage you to file an Azure Support Request with Microsoft Azure instead of filing a GitHub Issue in this repository. You can do so by going online to the Azure portal and submitting a support request. Access to subscription management and billing support is included with your Microsoft Azure subscription, and technical support is provided through one of the Azure Support Plans. For step-by-step guidance for the Azure portal, see How to create an Azure support request. Alternatively, you can create and manage your support tickets programmatically using the Azure Support ticket REST API

rajkumar-rangaraj commented 3 years ago

@stevieray8450 Could you please provide a small repro (an archive or a repo) that will help us to investigate this issue.

stevieray8450 commented 3 years ago

@rajkumar-rangaraj , sure. Please see https://github.com/stevieray8450/AiTest and supply your own instrumentation key in appsettings.json. Thank you!

cijothomas commented 3 years ago

https://github.com/stevieray8450/AiTest/blob/master/Program.cs#L20 The Logging provider is replaced with Serilog, so the logs don't get sent to ApplicationInsights.

stevieray8450 commented 3 years ago

@cijothomas Thanks for pointing that out. Yet dependency, metric, and request data gets recorded. It seems like I should be able to still collect that exception telemetry, too. How could I get that?

cijothomas commented 3 years ago

If you replace the logging system with Serilog, (as in the repro you shared), no logging provider can get the logs. (not just application insights, anything like ms.ext.logging.console). You can simply comment out the following lines (https://github.com/stevieray8450/AiTest/blob/master/Program.cs#L20-L25) to get logs flowing to ApplicationInsights (and any other providers).

If you want to keep Serilog as the main provider, and have serilog forward it to others (in the repro, it look like its forwarding to Console), then you need to configure Serilog to sent to ApplicationInsights (I have seen that they do have a sink for application insights as well). Its best to ask in Serilog repo on how to set up that.