Boas práticas do API Application Insights para usar
Eu li esta documentação:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics
Existem muitos métodos de API diferentes para rastrear exceções, rastrear rastreamentos etc.
Eu tenho um aplicativo ASP.NET MVC 5. Por exemplo, eu tenho o seguinte método de controlador (chamado pelo 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");
}
}
esse método lança AjaxException se algo errado (que é capturado por AjaxErrorHandling e, em seguida, retorna alguma resposta json ao cliente).
Agora, quero adicionar telemetria para registro, analisando exceções e observando eventos do cliente.
Então, eu adicionei o seguinte:
[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");
}
}
pela seguinte linha:
telemetryClient.TrackEvent("Sync driver", new Dictionary<string, string> { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() } }, null);
Eu apenas "log" evento do cliente, que o método foi chamado. Apenas para estatísticas.
Em cada bloco "catch", tento escrever rastreio com parâmetros diferentes e escrever exceção:
telemetryClient.TrackTrace("trace name", new Dictionary<string, string> {
{ "ChangedBy", User.Identity.Name },
....
});
telemetryClient.TrackException(ex);
Isso é necessário? Ou só precisa rastrear apenas a exceção? Então eu perco informações diferentes, como quem tenta adicionar essas alterações etc ... Quando cada um desses métodos deve ser usado?