ASP.NET MVC 3プロジェクトでは、グローバルHandleErrorアクションフィルターがデフォルトで次の場所に登録されますglobal.asax.cs。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
この属性はすべてのコントローラーアクションに適用され、カスタムエラーページのみにcustomErrors設定されている場合Onは表示され、コントローラーアクションで発生した例外は処理済みとしてマークされます。ASP.NET Health Monitoringはこの例外を認識しなくなり、ログに記録できなくなります。
HandleError属性およびカスタムエラーページと一緒にヘルスモニタリングを使用するアプローチは、こことこことここで説明されています:
以下から派生したカスタムエラー属性を作成しますHandleError。
public class HandleErrorHealthMonitoringAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// Do the default, i.e. show custom error page if custom errors are on
base.OnException(filterContext);
// Suppress raising the health monitoring event below if custom errors
// are off. In that case health monitoring will receive the exception
// anyway and raise the event
if (!filterContext.HttpContext.IsCustomErrorEnabled)
return;
// Raise health monitoring event
var errorEvent = new GenericWebRequestErrorEvent(
"Unhandled exception occurred.", this,
WebEventCodes.WebExtendedBase + 1, filterContext.Exception);
errorEvent.Raise();
}
}
そして、デフォルトの代わりにこの属性を登録しますHandleError:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorHealthMonitoringAttribute());
}
はGenericWebRequestErrorEvent、ベースから派生したカスタムエラーイベントWebRequestErrorEventです。カスタムは何も行わず、コンストラクターWebRequestErrorEventがないためにのみ存在するため、次を使用することはできません。publicvar errorEvent = new WebRequestErrorEvent(...)
public class GenericWebRequestErrorEvent : WebRequestErrorEvent
{
public GenericWebRequestErrorEvent(string message, object eventSource,
int eventCode, Exception exception) :
base(message, eventSource, eventCode, exception)
{
}
public GenericWebRequestErrorEvent(string message, object eventSource,
int eventCode, int eventDetailCode, Exception exception) :
base(message, eventSource, eventCode, eventDetailCode, exception)
{
}
}
MyNamespace.GenericWebRequestErrorEvent「あり」と「なし」というタイトルのメールが届きSystem.Web.Management.WebRequestErrorEvent、イベントコードは常に100001(= WebEventCodes.WebExtendedBase + 1)になることに注意してください。