1

我想将一些自定义错误消息以及引发的异常传递给 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");
            }

我能怎么做?

4

1 回答 1

3

覆盖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 .... 
}
于 2013-07-15T13:06:17.410 回答