API Application Insights, una buena práctica para usar
Leo esta documentación:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics
Hay muchos métodos API diferentes para rastrear excepciones, rastrear el rastreo, etc.
Tengo una aplicación ASP.NET MVC 5. Por ejemplo, tengo el siguiente método de controlador (llamado por 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");
}
}
este método arroja AjaxException si algo anda mal (lo que atrapa AjaxErrorHandling y luego devuelve algo de respuesta json al cliente).
Ahora quiero agregar telemetría para iniciar sesión, analizar excepciones y observar eventos de clientes.
Entonces, agregué lo siguiente:
[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");
}
}
por la siguiente línea:
telemetryClient.TrackEvent("Sync driver", new Dictionary<string, string> { { "ChangedBy", User.Identity.Name }, { "DriverID", DriverID.ToString() } }, null);
Acabo de "registrar" el evento del cliente, que se llamó al método. Solo para estadísticas.
En cada bloque "catch" trato de escribir trazas con diferentes parámetros y escribir excepción:
telemetryClient.TrackTrace("trace name", new Dictionary<string, string> {
{ "ChangedBy", User.Identity.Name },
....
});
telemetryClient.TrackException(ex);
¿Es necesario? ¿O solo necesita rastrear solo una excepción? Luego pierdo información diferente, como quién intenta agregar estos cambios, etc. ¿Cuándo se debe usar cada uno de estos métodos?