使用 EntLib 4.1 異常處理區塊 + 日誌區塊,將 Web 異常寫入 Windows 事件日誌。
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling、.LoggingApplication_Error:protected void Application_Error(Object sender, EventArgs e)
{
try {
Exception objErr = Server.GetLastError().GetBaseException();
Application["errorPage"] = Request.Url.ToString();
Application["errorMsg"] = objErr.Message;
Server.ClearError();
ExceptionPolicy.HandleException(objErr, "global expcetion");
} catch {}
}
web.config 設定異常策略:<exceptionHandling>
<exceptionPolicies>
<add name="global expcetion">
<exceptionTypes>
<add type="System.Exception, mscorlib..." postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="100" severity="Error"
title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter..."
priority="0" useDefaultLogger="false"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler..."
name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
throw new Exception("Test");IExceptionFilter 否則異常不會傳至 Application_Error;若必須用 Filter,請在 Filter 內呼叫 ExceptionPolicy.HandleException。