API Application Insights使用的良好实践

我阅读了这篇文档: https : //docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics

跟踪exception,跟踪跟踪等有许多不同的API方法。

我有一个ASP.NET MVC 5应用程序。 例如,我有以下控制器方法(由ajax调用):

[AjaxErrorHandling] [HttpPost] public async Task SyncDriverToVistracks(int DriverID) { if ([condition]) { // some actions here try { driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain); await db.SaveChangesAsync(); } catch (VistracksApiException api_ex) { // external service throws exception type VistracksApiException throw new AjaxException("vistracksApiClient", api_ex.Response.Message); } catch (VistracksApiCommonException common_ex) { // external service throws exception type VistracksApiCommonException throw new AjaxException("vistracksApiServer", "3MD HOS server is not available"); } catch (Exception ex) { // something wrong at all throw new AjaxException("General", ex.Message); } } else { // condition is not valid throw new AjaxException("General", "AccountId is not found"); } } 

如果出现错误(由AjaxErrorHandling捕获,然后返回json响应客户端),此方法抛出AjaxException。

现在我想添加遥测记录,分析exception并观察客户端事件。

所以,我添加了以下内容:

  [AjaxErrorHandling] [HttpPost] public async Task SyncDriverToVistracks(int DriverID) { telemetryClient.TrackEvent("Sync driver", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() } }, null); if ([condition]) { // some actions here try { driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain); await db.SaveChangesAsync(); } catch (VistracksApiException api_ex) { // external service throws exception type VistracksApiException telemetryClient.TrackTrace("VistracksApiException", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() }, { "ResponseCode", api_ex.Response.Code.ToString() }, { "ResponseMessage", api_ex.Response.Message }, { "ResponseDescription", api_ex.Response.Description } }); telemetryClient.TrackException(api_ex); throw new AjaxException("vistracksApiClient", api_ex.Response.Message); } catch (VistracksApiCommonException common_ex) { // external service throws exception type VistracksApiCommonException telemetryClient.TrackTrace("VistracksApiCommonException", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() }, { "Message", common_ex.Message }, }); telemetryClient.TrackException(common_ex); throw new AjaxException("vistracksApiServer", "3MD HOS server is not available"); } catch (Exception ex) { // something wrong at all telemetryClient.TrackTrace("Exception", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() }, { "Message", ex.Message }, }); telemetryClient.TrackException(ex); throw new AjaxException("General", ex.Message); } } else { telemetryClient.TrackTrace("ConditionWrong", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() }, { "Message", "AccountId is not found" }, }); // condition is not valid throw new AjaxException("General", "AccountId is not found"); } } 

通过以下行:

  telemetryClient.TrackEvent("Sync driver", new Dictionary { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() } }, null); 

我只是“记录”客户端事件,该方法被调用。 仅供统计。

在每个“catch”块中,我尝试使用不同的参数编写跟踪并编写exception:

  telemetryClient.TrackTrace("trace name", new Dictionary { { "ChangedBy", User.Identity.Name }, .... }); telemetryClient.TrackException(ex); 

有必要吗? 或者只是需要跟踪exception? 然后我失去了不同的信息,比如谁试图添加这些更改等…当应该使用这些方法中的每一个?

这是2.5.1 AI SDK的最佳实践。 将突出显示即将发布的AI SDK版本中可能不需要的部分。

进行端到端跟踪的正确方法是依赖.NET框架中的新Activity类。 在AI支持Activity.Tags( https://github.com/Microsoft/ApplicationInsights-dotnet/issues/562 )之前,您需要使用TelemetryInitializer手动传播它们:

 public class ActvityTagsTelemetryInitializer : ITelemetryInitializer { public void Initialize(ITelemetry telemetry) { Activity current = Activity.Current; if (current == null) { current = (Activity)HttpContext.Current?.Items["__AspnetActivity__"]; } while (current != null) { foreach (var tag in current.Tags) { if (!telemetry.Context.Properties.ContainsKey(tag.Key)) { telemetry.Context.Properties.Add(tag.Key, tag.Value); } } current = current.Parent; } } } 

然后在ApplicationInsights.config中注册它:

   ...   

然后你可以填充适当的标签:

 [AjaxErrorHandling] [HttpPost] public async Task SyncDriverToVistracks(int DriverID) { Activity.Current.AddTag("DriverID", DriverID.ToString()); Activity.Current.AddTag("UserID", User.Identity.Name); try { if ([condition]) { // some actions here try { // If below call is HTTP then no need to use StartOperation using (telemetryClient.StartOperation("AddNewDriverToVistrackAsync")) { driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain); } // If below call is HTTP then no need to use StartOperation using (telemetryClient.StartOperation("SaveChanges")) { await db.SaveChangesAsync(); } } catch (VistracksApiException api_ex) { // external service throws exception type VistracksApiException throw new AjaxException("vistracksApiClient", api_ex.Response.Message); } catch (VistracksApiCommonException common_ex) { // external service throws exception type VistracksApiCommonException throw new AjaxException("vistracksApiServer", "3MD HOS server is not available"); } catch (Exception ex) { // something wrong at all throw new AjaxException("General", ex.Message); } } else { // condition is not valid throw new AjaxException("General", "AccountId is not found"); } } catch (Exception ex) { // Upcoming 2.6 AI SDK will track exceptions for MVC apps automatically. telemetryClient.TrackException(ex); throw; } } 

你应该有以下遥测:

  1. 来电请求
  2. 传出请求(依赖关系)
  3. 失败请求的例外情况

所有遥测都将标有ChangedBy和DriverID

您可以单独跟踪所有指标/例外/跟踪/事件。 要使彼此相关的信息事件使用TelemetryContext

有必要吗? 或者只是需要跟踪exception? 然后我失去了不同的信息,比如谁试图添加这些更改等…当应该使用这些方法中的每一个?

这只取决于你的需求。 如果您需要该信息 – 发送它。