链接:https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#prior-versions-support
如果CustomErrors配置关闭,那么HTTP就可以收集异常。
//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{ //or reuse instance (recommended!). see note above
var ai = new TelemetryClient();
ai.TrackException(filterContext.Exception);
}如果是IsCustomErrorEnabled,那么它将跟踪异常。要使IsCustomErrorEnabled成为真,customError必须是打开的。那么,为什么它会说-如果customError关闭,那么AI HTTPModule将报告异常?
发布于 2022-07-26 08:45:00
我认为这个Microsoft示例中的评论有点误导人,以下是它的含义:
CustomErrors是Off,则Application模块将按预期处理所有异常。应用程序洞察HTTP模块应该出现在web.config <modules>部分CustomErrors是On,则Application模块将无法跟踪异常,这就是为什么我们需要使用自定义属性类进行解决的原因因此,属性示例代码使用语句if (filterContext.HttpContext.IsCustomErrorEnabled)避免两次记录异常:在自定义属性和Application模块中。
如果评论说的是这样的话,那就更清楚了:
//The attribute should track exceptions only when CustomErrors setting is On
//if CustomErrors is Off, exceptions will be caught by AI HTTP Module
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
var ai = new TelemetryClient();
ai.TrackException(filterContext.Exception);
}请注意,以上所有内容仅适用于MVC 4和以前的版本。从MVC 5开始,Application可以自动收集未处理的异常,不需要任何解决办法。
UPDATE:我建议对此文档页进行改进,它已经得到了Azure团队的批准。
https://stackoverflow.com/questions/73046558
复制相似问题