【问题标题】:MVC4: How to pass custom error log + exception to Elmah?MVC4:如何将自定义错误日志+异常传递给 Elmah?
【发布时间】:2013-07-15 13:00:58
【问题描述】:

我想将一些自定义错误消息与引发的异常一起传递给 elmah 源。例如我有课

public class Activity
{
   public string Id { get; set; }      
   public Committee Committee { get; set; }       
    public Contact Contact { get; set; }
    [Range(1950, 2050, ErrorMessage = "{0} must be between {1} and {2}.")]
    [DisplayName("Activity End Date")]
    public int? EndDate { get; set; }       
    [DisplayName("Source")]
    public string Source { get; set; }
    [DisplayName("Activity Role")]
    public string Role { get; set; }
    [DisplayName("Comments")]
    public string Comments { get; set; }
}

当引发异常时,我的异常中没有此类道具值。所以我想将这些值和异常传递给 elmah。

现在我在做

 catch (Exception exception)
            {

                Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
                return View("Error");
            }

我该怎么办?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 elmah


    【解决方案1】:

    在您的类中覆盖ToString() 方法并引发异常:

    catch (Exception exception)
    {
      var newException = new Exception(yourClass.ToString(), exception);
      Elmah.ErrorSignal.FromCurrentContext().Raise(newException);
    }
    

    在您的Activity 班级中:

    public override string ToString()
    {
       //return concatenate the property names and values here .... 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2011-05-22
      • 1970-01-01
      • 2015-06-14
      相关资源
      最近更新 更多