Asp.net web api 哪个Insights实现保证记录所有异常?
我们有一个Asp.net web api 哪个Insights实现保证记录所有异常?,asp.net-web-api,azure-application-insights,global-asax,.net-framework-4.8,Asp.net Web Api,Azure Application Insights,Global Asax,.net Framework 4.8,我们有一个global.asax.cs文件,其中包含此代码 方法一 public class WebApiApplication : System.Web.HttpApplication { TelemetryClient _telemetry = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration() { InstrumentationKey
global.asax.cs
文件,其中包含此代码
方法一
public class WebApiApplication : System.Web.HttpApplication
{
TelemetryClient _telemetry = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration() {
InstrumentationKey = EnvironmentHelper.InstrumentationKey,
ConnectionString = EnvironmentHelper.AppInsightsConnectionString
});
protected void Application_Start()
{
HttpConfiguration config = GlobalConfiguration.Configuration;
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
我担心这不会记录洞察
的所有异常。使用此代码会更好吗
方法二
public class WebApiApplication : System.Web.HttpApplication
{
TelemetryClient _telemetry = new TelemetryClient(...);
protected void Application_Start()
{
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Filters.Add(new CustomExceptionFilter()); // ADDED LINE
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_Error(Object sender, EventArgs e) // ADDED METHOD
{
Exception appException = Server.GetLastError();
_telemetry.TrackException(appException);
}
}
// ADDED CLASS
public class CustomExceptionFilter : ExceptionFilterAttribute
{
TelemetryClient _telemetry = new TelemetryClient(...);
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
_telemetry.TrackException(actionExecutedContext.Exception);
base.OnException(actionExecutedContext);
}
}
这些方法相同还是更可靠?不确定您的意思是什么
所有异常都会被记录下来
实际上,Application Insights
将自动收集WebAPI 2+的控制器方法中抛出的未处理异常,以下情况除外:
- 从控制器构造函数引发的异常
- 从消息处理程序引发的异常
- 路由过程中引发的异常
- 响应内容序列化期间引发的异常
- 应用程序启动期间引发异常
- 后台任务中引发异常
对于其他由应用程序处理的异常,仍然需要手动跟踪。您可以使用遥测客户端来跟踪这些异常
被引用的单据为