在 Insights 中看不到任何痕迹或异常
Not seeing any trace or exceptions in Insights
TL;DR: 这个 post 的标题可以是 "Why do all my Insights calls get logged as 'A task was canceled.'?"
我正在尝试使用 Application Insights
记录来自我们 ASP.NET web API 2
应用程序的错误,但它们显然没有被记录。我有一个 Insights 密钥,这显然正在被使用,因为我可以看到 REQUEST
和 DEPENDENCY
条目,但是我的自定义代码没有创建 EXCEPTION
或 TRACE
条目。
我想知道我是否没有看到全局抛出的异常,因为我没有任何 controller-level 异常处理 - 尽管我相信下面的代码应该足以捕获所有异常并记录它们(当它们不再在当地被捕获和处理时)...
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");
}
}
to 与我的问题类似,但我没有在 Insights 中看到任何跟踪语句或故意记录的异常。
我经常看到的是这个日志条目:
A task was canceled.
除了这个例外:
System.Threading.Tasks.TaskCanceledException:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
虽然我不明白 how/why 我故意抛出的异常会被翻译成这个(如果这是正在发生的事情)或者为什么我的 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)
看起来这整个混乱是一个错误分配变量的结果。非常令人沮丧,但很容易解决。当我从控制器方法中删除故意抛出的异常并且仍然记录问题而不是跟踪消息时发现了这一点 - 这些应该很容易接收。错误的代码是构造函数:
示例Controller.cs:
[CookieAuthentication, CookieSlidingExpiration, InsightsExceptionFilter]
public class SomeController : ApiController
{
private TelemetryClient ai = null;
public SomeController()
{
TelemetryClient ai = new TelemetryClient(); // WHY LOCAL???
}
[Route("api/v1/SomeEndpoint"), HttpGet]
public IHttpActionResult GetSomeEndpoint([FromUri]string aParameter)
{
ai.TrackTrace("test trace");
throw new Exception("test exception");
}
}
当然,将其更改为...
示例Controller.cs:
[CookieAuthentication, CookieSlidingExpiration, InsightsExceptionFilter]
public class SomeController : ApiController
{
private TelemetryClient ai = null;
public SomeController()
{
ai = new TelemetryClient();
}
[Route("api/v1/SomeEndpoint"), HttpGet]
public IHttpActionResult GetSomeEndpoint([FromUri]string aParameter)
{
ai.TrackTrace("test trace");
throw new Exception("test exception");
}
}
...解决了问题,我现在收到了预期的跟踪语句。
TL;DR: 这个 post 的标题可以是 "Why do all my Insights calls get logged as 'A task was canceled.'?"
我正在尝试使用 Application Insights
记录来自我们 ASP.NET web API 2
应用程序的错误,但它们显然没有被记录。我有一个 Insights 密钥,这显然正在被使用,因为我可以看到 REQUEST
和 DEPENDENCY
条目,但是我的自定义代码没有创建 EXCEPTION
或 TRACE
条目。
我想知道我是否没有看到全局抛出的异常,因为我没有任何 controller-level 异常处理 - 尽管我相信下面的代码应该足以捕获所有异常并记录它们(当它们不再在当地被捕获和处理时)...
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");
}
}
我经常看到的是这个日志条目:
A task was canceled.
除了这个例外:
System.Threading.Tasks.TaskCanceledException:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
虽然我不明白 how/why 我故意抛出的异常会被翻译成这个(如果这是正在发生的事情)或者为什么我的 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)
看起来这整个混乱是一个错误分配变量的结果。非常令人沮丧,但很容易解决。当我从控制器方法中删除故意抛出的异常并且仍然记录问题而不是跟踪消息时发现了这一点 - 这些应该很容易接收。错误的代码是构造函数:
示例Controller.cs:
[CookieAuthentication, CookieSlidingExpiration, InsightsExceptionFilter]
public class SomeController : ApiController
{
private TelemetryClient ai = null;
public SomeController()
{
TelemetryClient ai = new TelemetryClient(); // WHY LOCAL???
}
[Route("api/v1/SomeEndpoint"), HttpGet]
public IHttpActionResult GetSomeEndpoint([FromUri]string aParameter)
{
ai.TrackTrace("test trace");
throw new Exception("test exception");
}
}
当然,将其更改为...
示例Controller.cs:
[CookieAuthentication, CookieSlidingExpiration, InsightsExceptionFilter]
public class SomeController : ApiController
{
private TelemetryClient ai = null;
public SomeController()
{
ai = new TelemetryClient();
}
[Route("api/v1/SomeEndpoint"), HttpGet]
public IHttpActionResult GetSomeEndpoint([FromUri]string aParameter)
{
ai.TrackTrace("test trace");
throw new Exception("test exception");
}
}
...解决了问题,我现在收到了预期的跟踪语句。