Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights - Exceptions Add information

Actually, I have application insights set in my asp.net web API 2 to log the error following the steps Diagnose exceptions in your web apps with Application Insights, everything is working fine but, I want to add information for each an exception, for example, CustomerId, JobId, and other.

So I would like to see that data in the exceptions (either call stack or other property) through Azure Application Insights (see this image). That information could help me to detect what are the records that I should use to try to replicate the error scenario.

Can you tell me any recommendation?

Thanks!

like image 506
Carlos Bolivar Avatar asked Jun 04 '17 17:06

Carlos Bolivar


1 Answers

As far as I know, the TelemetryClient.TrackException could add custom properties.

After adding these properties, you could find values in the azure insights portal.

More details about how to add the custom properties in web api, you could refer to below codes:

  public class AiExceptionLogger : ExceptionLogger
        {
            public override void Log(ExceptionLoggerContext context)
            {
                if (context != null && context.Exception != null)
                {//or reuse instance (recommended!). see note above
                    var ai = new TelemetryClient();
                    //Here you need get the CustomerId, JobId, and other. For example get the current user id
                    string re = HttpContext.Current.User.Identity.Name;

                    // Set up some properties:
                   //Here you need get what you need and add them to the properties
                    var properties = new Dictionary<string, string> {{ "Users", "vvvvv" } };
                        // Send the exception telemetry:
                    ai.TrackException(context.Exception, properties);
                }
                base.Log(context);
            }
        }

You could find it in the See all properties as below:

enter image description here


But I have other question, How can I do to send data from my controller to "AiExceptionLogger". i.e: I have my controller a POST method Post(user, jobId), I want to add jobId to TrackException. Note: I don't want to use try{} catch(){} for every method in my controller, If I can add that information to context, it will be grat!. Thanks!

According to your description, I suggest you could try another way registered a filter and override the OnActionExecuted method.

In this method, you could firstly check the Exception is null. If the exception isn't null, you could get the ActionArguments from the HttpActionExecutedContext.

Then you could add this arguments in the properties and send them to the azure Application Insights.

More details, you could refer to below codes:

WebApiConfig:

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services   

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());

            config.Filters.Add(new AAA());
        }

        public class AAA : ActionFilterAttribute
        {

            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                if (actionExecutedContext.Exception != null)
                {
                    var ai = new TelemetryClient();
                     //here get the arguments
                    string d1 = (string)actionExecutedContext.ActionContext.ActionArguments["id"];
                    var properties = new Dictionary<string, string> { { "Users", d1 } };
                    ai.TrackException(actionExecutedContext.Exception, properties);
                }
                base.OnActionExecuted(actionExecutedContext);
            }
        }

Result:

enter image description hereenter image description here

like image 72
Brando Zhang Avatar answered Nov 15 '22 09:11

Brando Zhang