【发布时间】:2020-06-05 22:13:10
【问题描述】:
TL;DR:这篇文章的标题可以是“为什么我的所有 Insights 调用都被记录为'任务已取消。'?”
我正在尝试使用Application Insights 记录来自我们的ASP.NET web API 2 应用程序的错误,但它们显然没有被记录。我有一个 Insights 密钥,显然正在使用它,因为我可以看到 REQUEST 和 DEPENDENCY 条目,但是我的自定义代码没有创建 EXCEPTION 或 TRACE 条目。
我想知道我是否没有看到全局抛出的异常,因为我没有任何控制器级别的异常处理 - 尽管我相信下面的代码应该足以捕获所有异常并记录它们(当它们不是更多本地捕获和处理)...
Global.asax.cs:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = EnvironmentHelper.InsightsKey;
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_Error(Object sender, EventArgs e)
{
Exception appException = Server.GetLastError();
var ai = new TelemetryClient();
ai.TrackException(appException);
}
}
WebApiConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Services.Replace(typeof(IExceptionLogger), new InsightsExceptionLogger());
GlobalConfiguration.Configuration.Filters.Add(new InsightsExceptionFilter());
}
}
InsightsExceptionLogger.cs:
public class InsightsExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
var ai = new TelemetryClient();
ai.TrackException(context.Exception);
}
base.Log(context);
}
}
public class InsightsExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context != null && context.Exception != null)
{
var ai = new TelemetryClient();
ai.TrackException(context.Exception);
}
}
public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
{
if (context != null && context.Exception != null)
{
var ai = new TelemetryClient();
ai.TrackException(context.Exception);
}
return Task.FromResult(0);
}
}
示例 Controller.cs:
[CookieAuthentication, CookieSlidingExpiration, InsightsExceptionFilter]
public class SomeController : ApiController
{
private TelemetryClient ai = null;
public SomeController()
{
TelemetryClient ai = new TelemetryClient();
}
[Route("api/v1/SomeEndpoint"), HttpGet]
public IHttpActionResult GetSomeEndpoint([FromUri]string aParameter)
{
ai.TrackTrace("test trace");
throw new Exception("test exception");
}
}
first answer 到 this post 与我的问题类似,但我在 Insights 中没有看到任何跟踪语句或故意记录的异常。
我经常看到的是这个日志条目:
A task was canceled.
除了这个例外:
System.Threading.Tasks.TaskCanceledException:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
虽然我不明白我故意抛出的异常如何/为什么会被翻译成这个(如果发生了这种情况),或者为什么我的 TrackTrace() 调用没有出现在 Insights 日志中。
在那个异常下,我看到了这个堆栈跟踪:
System.Threading.Tasks.TaskCanceledException:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.ActionFilterResult+<ExecuteAsync>d__5.MoveNext (System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.AuthenticationFilterResult+<ExecuteAsync>d__5.MoveNext (System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.ExceptionFilterResult+<ExecuteAsync>d__6.MoveNext (System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
【问题讨论】:
标签: asp.net-web-api2 azure-application-insights