我阅读了这份文档:https ://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics
有许多不同的 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。
现在我想添加遥测来记录、分析异常和观察客户端事件。
所以,我添加了以下内容:
[AjaxErrorHandling]
[HttpPost]
public async Task SyncDriverToVistracks(int DriverID)
{
telemetryClient.TrackEvent("Sync driver", new Dictionary<string, string> { { "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<string, string> {
{ "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<string, string> {
{ "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<string, string> {
{ "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<string, string> {
{ "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<string, string> { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() } }, null);
我只是“记录”客户端事件,即调用了该方法。只是为了统计。
在每个“catch”块中,我尝试使用不同的参数编写跟踪并写入异常:
telemetryClient.TrackTrace("trace name", new Dictionary<string, string> {
{ "ChangedBy", User.Identity.Name },
....
});
telemetryClient.TrackException(ex);
有必要吗?还是只需要跟踪异常?然后我会丢失不同的信息,比如谁尝试添加这些更改等等......什么时候应该使用这些方法中的每一个?