我正在编写自定义异常类,并在需要时使用它们登录elmah。
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);
}
}
}
我正在使用
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);
}
}
如何使用上述异常消息,堆栈跟踪等,以便我不会吃掉“异常”消息。
我希望也可以通过读取elmah日志来记录异常的确切细节,例如,如果它是实体框架异常或空引用异常等。
更新
我早先确定我的代码我得到了MyCustomLogException作为异常记录和消息“使用Property1 = {0},Property2 = {1}和Property3 = {2}”作为异常消息记录到“db的错误”。这种方式只需通过读取日志我可以找出错误,然后可能会读取内部异常以获取更多详细信息。 - Yasser 8分钟前
现在使用new code我的案例中的实际异常类Sql被记录并且异常消息“违反UNIQUE KEY约束'IX_abc_log'。无法在对象'dbo.abc'中插入重复键。”声明已被终止。“记录,我的自定义异常类和消息记录为内部异常
答案 0 :(得分:3)
在编写自定义异常时,您的自定义异常应如下所示:
[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) { }
}
你会看到现在Exception可以在它的构造函数中使用内部异常,这就是你正在寻找的。 p>
如果要添加属性,可以再添加一个构造函数,如下所示:
public CustomException(string message, Exception innerException, string property1, string property2)
: base(message, innerException) {
// Do whatever you want with the extra properties here.
}
编辑提问者希望看到自定义异常消息,因此CustomException被修改为:
[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);
}
}
}