2014-10-08 66 views
0

我正在编写自定义异常类并使用它们在需要时登录到elmah。如何在c中编写自定义异常时使用内部异常#

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'中插入重复键' 。 该语句已终止。”登录和我的自定义异常类和消息会被记录为内部异常

回答

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) { } 
} 

你会看到,现在的异常可以在它的构造函数中接受一个内部异常,这就是你要找的。

当你想添加的属性,你可以添加更多的构造函数,它看起来像:

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

编辑的questionater,希望看到一个自定义异常消息,因此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); 
     } 
    } 
} 
+0

谢谢。这看起来不错,但是当我使用像这样写的CustomException类不会显示在elmah页面中时,会记录'actual'异常类和​​消息,并且我的自定义异常会记录为内部异常。这是例外行为吗? – Yasser 2014-10-08 06:45:10

+1

你能澄清一点吗?你现在拥有什么,什么是预期的。也许在原始问题下添加“更新”部分。我这样问,因为对于我来说,目前还不清楚你现在在写什么...... – Complexity 2014-10-08 06:47:50

+0

不像你已经建议的那样添加注释,而是在原始问题中添加一个'更新'部分。这应该提到你当前的实现,这个呈现的输出以及你的期望。如果他们正在阅读你的问题,这使得其他人更清楚。评论部分未定义为编写代码。如果您更新您的问题,我很乐意提供帮助。 – Complexity 2014-10-08 06:52:05