代码之家  ›  专栏  ›  技术社区  ›  Catalin

覆盖Elmah记录的错误消息

  •  3
  • Catalin  · 技术社区  · 12 年前

    是否有任何方法覆盖记录的错误消息 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));
            }
        }
    }
    

    enter image description here

    2 回复  |  直到 12 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    我想你的问题可能在这里:

        if (businessException != null) {
            ErrorSignal.FromCurrentContext().Raise(
                new Exception(businessException.InternalErrorMessage, exception));
        }
    

    当您创建一个新的异常而不是像这样的异常时:

        if (businessException != null) {
            ErrorSignal.FromCurrentContext().Raise(businessException));
        }
    

    我已经为我的一个网站编写了代码,可以看看今天晚些时候是否可以重新创建您的问题(假设这不起作用)。我认为这是SO职位帮助我实现的: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

    编辑:

    重读你的问题和另一个答案,我意识到我在试图解决你试图纠正的问题,而不是实际问题。你有没有试过 this solution 这与您当前的尝试略有不同:

    public override void OnException(ExceptionContext filterContext) {
        if (filterContext.ExceptionHandled == true) {
            return;
        }
        Exception exception = filterContext.Exception;
    
        BusinessException businessException = exception as BusinessException;
        if (businessException != null) {
            var customEx = new Exception(
                businessException.InternalErrorMessage, new BusinessException());
            ErrorSignal.FromCurrentContext().Raise(customEx);
            return;
        }
    }
    

    检查 InternalErrormessage 正在返回您期望的结果,我希望 return 这里将防止异常被记录两次。否则,这基本上就是你所做的。

        2
  •  0
  •   Simon Halsey    12 年前

    我怀疑您需要创建自己的错误日志实现来记录标准财产以外的任何内容。这应该不会太难。

    使用 SqlErrorLog 例如,您只需要重写log方法&输入您自己的逻辑来修改 Error 类在调用基实现之前包含。

    使用@Matthew所说的内容将停止两次记录异常。

    所有源代码都是 here