1

I am writing custom exception class and using them to log to elmah whenever required.

public class MyCustomLogException : Exception
{
    public string Property1 { get; protected set; }
    public string Property2 { get; protected set; }
    public string Property3 { get; protected set; }

    internal MyCustomLogException(string property1, string property2, string property3)
    {
        Property1 = property1;
        Property2 = property2;
        Property3 = property3;        
    }

    public override string Message
    {
        get
        {
            return string.Format("Error logging to db using Property1 = {0}, Property2 = {1} and Property3 = {2}", Property1, Property2, Property3);
        }
    }
}

and I am using

public int LogSomethingToDb(SomeModel log)
{
    try
    {
        // log to db
    }
    catch(Exception exception)
    {
        // to do how to use this exception ?
        throw new MyCustomLogException(log.Property1, log.Property2, log.Property3);
    }
}

How do I use the above exception message, stack trace etc so that I dont end up eating the 'exception' message.

I want the exact detail of the exception to also be logged for eg if its a entity framework exception or a null ref exception etc just by reading the elmah logs.

Update

Yea sure earlier with my code i was getting MyCustomLogException as the exception logged and message of "Error logging to db using Property1 = {0}, Property2 = {1} and Property3 = {2}" as the exception message. This way just by reading the log I can figure out what is wrong and then may be read the inner exception to get more details. – Yasser 8 mins ago

Now with the new code the actual exception class Sql in my case is getting logged and the exception message of "Violation of UNIQUE KEY constraint 'IX_abc_log'. Cannot insert duplicate key in object 'dbo.abc'. The statement has been terminated." is logged and my custom exception class and message are logged as inner exception

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281

1 Answers1

3

When you are writing custom exceptions, your custom exception should look like this as minimum:

[Serializable]
public class CustomException : Exception
{
    public CustomException()
        : base() { }

    public CustomException(string message)
        : base(message) { }

    public CustomException(string format, params object[] args)
        : base(string.Format(format, args)) { }

    public CustomException(string message, Exception innerException)
        : base(message, innerException) { }

    public CustomException(string format, Exception innerException, params object[] args)
        : base(string.Format(format, args), innerException) { }

    protected CustomException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }
}

You'll see that now the Exception can take an inner exception in it's constructor and this is what you're looking for.

When you want to add your properties, you can add one more constructor which looks like:

    public CustomException(string message, Exception innerException, string property1, string property2)
        : base(message, innerException) { 
    // Do whatever you want with the extra properties here.    
}

EDIT The questionater, would like to see a custom exception message, therefore the CustomException is modified like:

[Serializable]
public class CustomException : Exception
{
    public CustomException()
        : base() { }

    public CustomException(string message)
        : base(message) { }

    public CustomException(string format, params object[] args)
        : base(string.Format(format, args)) { }

    public CustomException(string message, Exception innerException)
        : base(message, innerException) { }

    public CustomException(string format, Exception innerException, params object[] args)
        : base(string.Format(format, args), innerException) { }

    protected CustomException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }

    // Added a custom constructor to allow adding the custom properties:
    internal CustomException(string message, Exception innerException, string property1, string property2, string property3) 
        : base(message, innerException) { }
    {
        Property1 = property1;
        Property2 = property2;
        Property3 = property3;        
    }

    // 3 properties holding all the values of the properties passed to it.
    public string Property1 { get; protected set; }
    public string Property2 { get; protected set; }
    public string Property3 { get; protected set; }

    // Override the message to allow custom exception message to be passed to Elmah.
    public override string Message
    {
        get
        {
            return string.Format("Error logging to db using Property1 = {0}, Property2 = {1} and     Property3 = {2}", Property1, Property2, Property3);
        }
    }
}
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • Thanks. This looks good, but when I use write like this the CustomException class is not shown in the elmah page, the 'actual' exception class and message is logged and my custom exception is logged as inner exception. Is this the excepted behaviour ? – Yasser Shaikh Oct 08 '14 at 06:45
  • 1
    Can you clarify a bit more. What do you have now, what is expected. Maybe add an 'updated' section under your original question. I'm asking this because to me it's not quite clear what you're doing now with the Write... – Complexity Oct 08 '14 at 06:47
  • Instead of adding comments, as suggested already, add an 'Updated' section in your original question. This should mentions your current implementation, which output this renders, and what you expect. This make's it more clear for other people if they are reading your question. The comments section is not defined for writing code. I'll be glad to help if you update your question. – Complexity Oct 08 '14 at 06:52
  • Thanks for your update, but you do not show how you construct the new CustomException, you mind showing that? – Complexity Oct 08 '14 at 07:02
  • The custom exception `MyCustomLogException` and the call to it both are already in the question. Thanks – Yasser Shaikh Oct 08 '14 at 07:12
  • It would be kind to let me know if the solution is working right now. – Complexity Oct 08 '14 at 07:46