James123
James123

Reputation: 11662

MVC4: How to pass custom error log + exception to Elmah?

I would like to pass some custom error message along with raised exception to elmah source. for example I have class

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; }
}

When exception raised, I don't have this class prop values in my exception. So I want pass these values and exception to elmah.

Now I am doing

 catch (Exception exception)
            {

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

How can I do?

Upvotes: 1

Views: 1303

Answers (1)

Icarus
Icarus

Reputation: 63962

Override the ToString() method in your class and raise the Exception as so:

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

In your Activity class:

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

Upvotes: 3

Related Questions