【发布时间】:2014-04-07 04:24:13
【问题描述】:
有什么方法可以覆盖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));
}
}
}
【问题讨论】:
标签: asp.net asp.net-mvc error-handling elmah