简体   繁体   中英

Application insight log exceptions only

I want to use Application Insights to log exceptions only. How can I do that?

I tried searching for ways turning other settings off such as this and it says that there is no way to turn it off.

I tried ITelemetryProcessor and encountered the same problem as this question . I tried both config and code ways of registering ITelemetryProcessor but it is not hit even if I explicitly throw an exception in my Web API controller.

I am using VS 2017 and created a new .Net Framework 4.6.2 Web API. I also have an InstrumentationKey and can see the exception logged in Azure portal.

First of all, the first link you referenced is nothing to do with your issue. You want to only log the exceptions, but that link means that remove the old telemetry data like Trace in repository(where the telemetry data is stored after upload to app insights).

You can take use of ITelemetryProcessor to log exceptions only. Please follow my steps as below:

1.Add Application insights to your web api project by right clicking your project name -> select Configure Application Insights : 在此处输入图片说明

After SDK added, do not select the Enable trace collection : 在此处输入图片说明

2.Add a .cs file in your project, then implement your custom ITelemetryProcessor class, code is as below:

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.Register your custom ITelemetryProcessor in the ApplicationInsights.config. In the node, add <Add Type="WebApplicationWebApi.ExceptionsFilter,WebApplicationWebApi"/> : 在此处输入图片说明

4.Then run your code. To make sure the custom ITelemetryProcessor class is called, you can set a breakpoint in that class to see if it's hit when running.

And for the testing purpose, I add some telemetry data in the 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.In your visual studio output window, you should see these messages: 在此处输入图片说明

6.Then in visual studio, nav to Application Insights Search (in vs -> view -> other windows -> Application Insights Search), then check if there are some values here(if it has values like "4" in screenshot below, click on it): 在此处输入图片说明

7.If it has values in step 6, please click the update button , then check All : 在此处输入图片说明

8.Then you can see that only the Exceptions are logged: 在此处输入图片说明

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