应用洞察 - 异常 添加信息

9
实际上,我已经在我的asp.net web API 2中设置了应用程序洞察来记录错误,按照使用应用程序洞察诊断Web应用程序中的异常的步骤进行。一切正常,但是,我想为每个异常添加信息,例如CustomerId,JobId等。
因此,我希望通过Azure应用程序洞察(请参见此图像)在异常中看到这些数据(无论是调用堆栈还是其他属性)。这些信息可以帮助我检测应该用于尝试复制错误场景的记录。
你能告诉我任何建议吗?
谢谢!

3
阅读有关 TelemetryInitializers 的信息,请参见 https://learn.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer。您可以向所有请求、异常等项添加自定义属性。但注入正确的值则需要由您来完成。您可以创建一个工厂类,为您的 JobId 等信息提供注入方法,具体取决于您如何访问这些信息。 - Peter Bons
1个回答

11
据我所知,TelemetryClient.TrackException可以添加自定义属性。
在添加这些属性之后,您可以在Azure Insights门户中找到相应的值。
有关如何在Web API中添加自定义属性的更多详细信息,请参考以下代码:
  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);
            }
        }
您可以在以下的“查看所有属性”中找到它:

enter image description here


但我有另一个问题,我该如何将数据从我的控制器发送到“AiExceptionLogger”呢?例如:我在我的控制器上有一个POST方法Post(user,jobId),我想将jobId添加到TrackException。 注意:我不想为控制器中的每种方法使用try()catch(),如果我可以将这些信息添加到context中,那将是很好的!谢谢!

根据您的描述,我建议您尝试另一种方法,即注册一个过滤器并重写OnActionExecuted方法。

在此方法中,您可以首先检查异常是否为空。如果异常不为空,您可以从HttpActionExecutedContext获取ActionArguments。

然后你可以将这些参数添加到属性中并将它们发送到 Azure 应用程序洞察。

更多详细信息,您可以参考以下代码:

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);
            }
        }

结果:

输入图像描述输入图像描述


谢谢你的回答,非常好!但我还有一个问题,我该如何将数据从我的控制器发送到"AiExceptionLogger"呢?例如:我有一个名称为Post(user, jobId)的控制器POST方法,我想将jobId添加到TrackException中。注意:我不想在控制器中的每个方法中都使用try {} catch(){},如果我可以将信息添加到上下文中,那就太好了!谢谢! - Carlos Bolivar
有更新了吗?如果您觉得我的回答有用/有帮助,请将其标记为答案,以便其他人也能从中受益。 - Brando Zhang
谢谢,Brando!太棒了,我要测试一下那个例子。 - Carlos Bolivar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接