c# - 仅应用程序洞察日志异常

标签 c# azure azure-application-insights

我只想使用 Application Insights 来记录异常。我怎样才能做到这一点?

我尝试寻找关闭其他设置的方法,例如 this并且它说无法将其关闭。

我尝试了 ITelemetryProcessor 并遇到了与此相同的问题 question 。我尝试了注册 ITelemetryProcessor 的配置和代码方法,但即使我在 Web API Controller 中明确抛出异常,它也不会被命中。

我正在使用 VS 2017 并创建了一个新的 .Net Framework 4.6.2 Web API。我还有一个 InstrumentationKey,可以看到 Azure 门户中记录的异常。

最佳答案

首先第一个link你提到的与你的问题无关。 您只想记录异常,但 link意味着删除旧的遥测数据,例如存储库中的 Trace(遥测数据上传到应用洞察后存储在其中)。

您可以使用 ITelemetryProcessor 仅记录异常。请按照以下步骤操作:

1.通过右键单击您的项目名称 -> 选择 Configure Application Insights 将应用程序洞察添加到您的 Web api 项目: enter image description here

添加SDK后,不要选择Enable trace collection : enter image description here

2.在您的项目中添加一个.cs文件,然后实现您自定义的ITelemetryProcessor类,代码如下:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace WebApplicationWebApi
{
    public class ExceptionsFilter:ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }
        public ExceptionsFilter(ITelemetryProcessor next)
        {
            this.Next = next;
        }

        public void Process(ITelemetry item)
        {
            string s = item.GetType().Name;

            //if it's not exception telemetry, just return without log it to app insights.
            if (s != "ExceptionTelemetry")
            {
                return;
            }            

            this.Next.Process(item);
        }

    }
}

3. 在 ApplicationInsights.config 中注册您的自定义 ITelemetryProcessor。在节点中添加 <Add Type="WebApplicationWebApi.ExceptionsFilter,WebApplicationWebApi"/> : enter image description here

4.然后运行您的代码。为了确保调用自定义 ITelemetryProcessor 类,您可以在该类中设置断点以查看运行时是否命中。

出于测试目的,我在 HomeController.cs 中添加了一些遥测数据:

public class HomeController : Controller
{
   TelemetryClient client = new TelemetryClient();
   public ActionResult Index()
   {
      RequestTelemetry r1 = new RequestTelemetry();
      r1.Name = "request message for testing";
      client.TrackRequest(r1);
      client.TrackTrace("trace message for testing wwwww.");
      client.TrackException(new Exception("exception message for testing wwwww."));
      ViewBag.Title = "Home Page";

      return View();
   }
}

5.在 Visual Studio 输出窗口中,您应该看到以下消息: enter image description here

6.然后在 Visual Studio 中,导航至 Application Insights Search (在 vs -> 查看 -> 其他窗口 -> Application Insights 搜索中),然后检查这里是否有一些值(如果有像下面屏幕截图中的“4”这样的值,请单击它): enter image description here

7.如果第6步中有数值,请点击update button ,然后检查All : enter image description here

8.然后您可以看到仅记录了异常: enter image description here

关于c# - 仅应用程序洞察日志异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52876721/

相关文章:

c# - 如何将计数添加到 Winform 中的按钮单击

c# - 在 C# 中编辑文本文件的特定行

.net - 将 .NET Windows 服务转换为 Azure Web 作业

Azure 备份保留策略

azure - 是否可以在 Azure 托管应用程序中为自定义资源类型使用自定义图标?

Azure 函数请求跟踪

c# - 将 Hangfire 作业记录到 Application Insights 并将事件与操作 ID 相关联

c# - 从父类继承而无需重新实现父类的子类接口(interface)

c# - 如何将列表中的字符串与句子中的单词匹配?

azure-application-insights - 停止 Application Insights,包括操作名称中的路径参数