首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用程序洞察-记录异常

应用程序洞察-记录异常
EN

Stack Overflow用户
提问于 2016-01-28 05:55:34
回答 3查看 23K关注 0票数 10

我在我的应用程序中为Application Insights编写了一个自定义记录器。在Azure门户中查看App Insights时,我看不到任何异常或事件。这是记录器类代码,当我调试代码时,我确实看到一个分配给InstrumentationKey属性的键,你知道我做错了什么吗?是否需要将其他信息附加到客户端或配置?

代码语言:javascript
复制
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);
    }
}
EN

回答 3

Stack Overflow用户

发布于 2016-01-29 02:05:35

我创建了一个新的控制台应用程序,安装了最新稳定的ApplicationInsights软件开发工具包,并且基本上保留了您的示例,但差别很小但很重要-我要么让它在调用TrackException后等待关闭,要么添加TelemetryClient.Flush()

代码语言:javascript
复制
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();
        }
    }
}

首先,我可以在Visual Studio调试输出窗口中看到发送的遥测项:

然后我可以看到遥测在Fiddler中离开我的机器,我还可以看到它被我们的数据收集端点成功接受。

最后,我可以在门户中看到它:

票数 13
EN

Stack Overflow用户

发布于 2019-06-17 17:50:31

如今,这个问题和公认的答案已经过时了。目前的方法是添加Microsoft.ApplicationInsights.AspNet NuGet包,它具有开箱即用的日志记录功能。

请参考the official docs

文档中的要点:

通过default.

  • Captured启用
  1. ApplicationInsightsLoggerProvider可以在appsettings.json中配置日志类型(如果确实需要,也可以通过编程方式进行配置)

代码语言:javascript
复制
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Error"
            }
        }
    }
票数 7
EN

Stack Overflow用户

发布于 2021-03-23 10:59:35

对于版本3的well作业,我尝试实例化TelemetryClient并调用TrackException(),以及调用Flush()并等待180秒,但这些都不起作用。起作用的是使用ILogger记录器对象并将异常传递给LogError()。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35048825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档