有沒有任何方法覆蓋Elmah
記錄的錯誤消息而不復制它?覆蓋Elmah記錄的錯誤消息
我有一個自定義異常類:
public class BusinessException : Exception
{
// detailed error message used for logging/debugging
public string InternalErrorMessage { get; set; }
public BusinessException(string message, string internalMessage)
:base(message)
{
InternalErrorMessage = internalMessage;
}
}
從代碼,我拋出一個異常這樣的:
string detailedErrorMessage = string.Format("User {0} does not have permissions to access CreateProduct resource", User.Identity.Name);
throw new BusinessException("Permission denied", detailedErrorMessage);
當ELMAH記錄錯誤,它只是記錄Permission denied
消息。但是,我需要記錄異常的InternalErrorMessage
屬性。
我試圖創建一個自定義HandleErrorAttribute
要做到這一點,但這種複製的例外記錄:
public class ErrorHandleAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled == true)
return;
Exception exception = filterContext.Exception;
BusinessException businessException = exception as BusinessException;
if (businessException != null)
{
ErrorSignal.FromCurrentContext().Raise(new Exception(businessException.InternalErrorMessage, exception));
}
}
}
這不是我希望的解決方案,但我想我沒有別的選擇 – Catalin