使用 Application Insights 跟踪非 HTTP 请求
Track non-HTTP requests with Application Insights
如本 link 中所述,在 .net core2.1 上 web.api 应用程序实现了以下代码以使用 Application Insights 跟踪非 HTTP 请求(即,redis 调用)
private ReviewModel<T> GetCachedData<T>(string id)
{
try
{
var obj = default(RedisValue);
_retryPolicy.Execute(() =>
{
using (var getReviewsOperation = _telemetryClient.StartOperation<DependencyTelemetry>("RedisCall"))
{
obj = _database.StringGet(id); // external call to redis server
}
});
var review = string.IsNullOrEmpty(obj) || !obj.HasValue ?
default(ReviewModel<T>) : _serializer.Deserialize<ReviewModel<T>>(obj );
return review;
}
catch (Exception ex)
when (ex is RedisException)
{
//how to log the exception cases in dependency table of application insights log?
_log.LogError($"Redis exception occurred : ", {ex.Message});
return null;
}
}
以上代码成功记录了应用程序洞察日志“依赖项”table 上的 redis 调用详细信息。但是如何在应用程序洞察的“依赖性”table 上记录 redis 调用详细信息,以成功 属性 值为“false”的异常情况登录?
You can use TrackDependency
for this. TrackDependency
用于跟踪调用外部代码的响应时间和成功率。结果显示在门户的依赖关系图中。在进行依赖调用的任何地方都需要添加下面的代码片段:
var success = false;
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
try
{
success = dependency.Call();
}
catch(Exception ex)
{
success = false;
telemetry.TrackException(ex);
throw new Exception("Operation went wrong", ex);
}
finally
{
timer.Stop();
telemetry.TrackDependency("DependencyType", "myDependency", "myCall", startTime, timer.Elapsed, success);
}
如本 link 中所述,在 .net core2.1 上 web.api 应用程序实现了以下代码以使用 Application Insights 跟踪非 HTTP 请求(即,redis 调用)
private ReviewModel<T> GetCachedData<T>(string id)
{
try
{
var obj = default(RedisValue);
_retryPolicy.Execute(() =>
{
using (var getReviewsOperation = _telemetryClient.StartOperation<DependencyTelemetry>("RedisCall"))
{
obj = _database.StringGet(id); // external call to redis server
}
});
var review = string.IsNullOrEmpty(obj) || !obj.HasValue ?
default(ReviewModel<T>) : _serializer.Deserialize<ReviewModel<T>>(obj );
return review;
}
catch (Exception ex)
when (ex is RedisException)
{
//how to log the exception cases in dependency table of application insights log?
_log.LogError($"Redis exception occurred : ", {ex.Message});
return null;
}
}
以上代码成功记录了应用程序洞察日志“依赖项”table 上的 redis 调用详细信息。但是如何在应用程序洞察的“依赖性”table 上记录 redis 调用详细信息,以成功 属性 值为“false”的异常情况登录?
You can use TrackDependency
for this. TrackDependency
用于跟踪调用外部代码的响应时间和成功率。结果显示在门户的依赖关系图中。在进行依赖调用的任何地方都需要添加下面的代码片段:
var success = false;
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
try
{
success = dependency.Call();
}
catch(Exception ex)
{
success = false;
telemetry.TrackException(ex);
throw new Exception("Operation went wrong", ex);
}
finally
{
timer.Stop();
telemetry.TrackDependency("DependencyType", "myDependency", "myCall", startTime, timer.Elapsed, success);
}