Application Insights - Logging exceptions

怎甘沉沦 提交于 2019-12-30 08:33:07

问题


I wrote a custom logger for Application Insights in my app. I don't see any exceptions or ANY events when viewing App Insights in Azure Portal. Here is the logger class code, when I debug the code I do see a key assigned to the InstrumentationKey property, any ideas what I am doing wrong here? Do I need to attach other info to the client or configuration?

public class AppInsightsLogger:ILogger
{
    private TelemetryClient ai;

    public AppInsightsLogger()
    {
        ai = new TelemetryClient();
        if (string.IsNullOrEmpty(ai.InstrumentationKey))
        {
            // attempt to load instrumentation key from app settings
            var appSettingsTiKey = AppSettings.InsightsKey;
            if (!string.IsNullOrEmpty(appSettingsTiKey))
            {
                TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey;
                ai.InstrumentationKey = appSettingsTiKey;
            }
            else
            {
                throw new Exception("Could not find instrumentation key for Application Insights");
            }
        }
    }
    public void LogException(Exception ex)
    {
        ai.TrackException(ex);
    }
}

回答1:


I created a new Console Application, installed latest stable ApplicationInsights SDK and pretty much kept your example, with minor but important difference - I either let it wait before shutting down after calling TrackException or added TelemetryClient.Flush()

namespace logtest
{
    class Program
    {
        static void Main(string[] args)
        {
            AppInsightsLogger logger = new AppInsightsLogger();
            logger.LogException(new InvalidOperationException("Is data showing?"));

            // either wait for a couple of minutes for the batch to be sent of add ai.Flush() after ai.TrackException() to send the batch immediately
            Console.ReadLine();
        }
    }

    public class AppInsightsLogger
    {
        private TelemetryClient ai;

        public AppInsightsLogger()
        {
            ai = new TelemetryClient();
            if (string.IsNullOrEmpty(ai.InstrumentationKey))
            {
                // attempt to load instrumentation key from app settings
                var appSettingsTiKey = "<ikey>";
                if (!string.IsNullOrEmpty(appSettingsTiKey))
                {
                    TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey;
                    ai.InstrumentationKey = appSettingsTiKey;
                }
                else
                {
                    throw new Exception("Could not find instrumentation key for Application Insights");
                }
            }
        }
        public void LogException(Exception ex)
        {
            ai.TrackException(ex);
            // ai.Flush();
        }
    }
}

First I could see telemetry item sent in Visual Studio debug output window:

Then I could see telemetry leaving my machine in Fiddler, I also could see it was successfully accepted by our data collection endpoint.

And finally I could see it in the portal:




回答2:


Nowadays, the question and the accepted answer are outdated. The contemporary approach is to add Microsoft.ApplicationInsights.AspNet NuGet package, which has logging out-of-the-box.

Please refer to the official docs.

The gist from the docs:

  1. ApplicationInsightsLoggerProvider is enabled by default.
  2. Captured log types can be configured in appsettings.json (or programmatically if you really need it)
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Error"
            }
        }
    }


来源:https://stackoverflow.com/questions/35048825/application-insights-logging-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!