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