有什么方法可以覆盖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));
}
}
}
我怀疑您需要创建自己的错误日志实现来记录除标准属性以外的任何内容。这应该不太困难。
使用SqlErrorLog
作为一个例子,你只需要重写log方法和把自己的逻辑来修改一下Error
类调用基实现之前包含。
使用@Matthew还说过的话,将使您停止两次记录该异常。
所有的源代码都在这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句