简体   繁体   English

elmah.axd 自定义异常消息,在 C# 中包含子抛出异常的堆栈跟踪

[英]elmah.axd custom exception message with stacktrace of child throwed exception in C#

What I want to achieve is to show a custom exception Type and Error message in elmah.axd table but with the original stacktrace of a child throwed exception.我想要实现的是在 elmah.axd 表中显示自定义异常TypeError消息,但带有子抛出异常的原始堆栈跟踪。 在此处输入图像描述 This is just an example of a nested try catch that match my needs:这只是一个符合我需要的嵌套 try catch 示例:

// custom exception constructor
public MyCustomException(string customMsg, Exception expt):base(customMsg,expt)

// my test case
try{
    try{
        //_context.saveChanges(); --> this will generate an exception
        // but for this example we'll throw the following
        throw new IndexOutOfRangeException();
    }
    catch (Exception e){
        // here elmah will print in the Type column "IndexOutOfrange" and in the Error column the message: "Index was outside the bounds of the array. Details..."
        ErrorSignal.FromCurrentContext().Raise(e);

        // now I throw a custom exception with the original stacktrace of "IndexOutOfrangeException"
        throw new MyCustomException("Message to see in elmah 'Error' column", e)
    }
}
catch(MyCustomException cex){
    // here elmah will also print in the Type column "IndexOutOfrange" and in the Error column the message: "Index was outside the bounds of the array. Details..." with the original stacktrace
    ErrorSignal.FromCurrentContext().Raise(cex)

    // my expectation would be to print in the Type column "MyCustomException" and in the Error column the message: "Message to see in elmah 'Error' column Details..." with the original stacktrace
}
catch(Exception ex){
    // some code here
}

Am I doing something wrong or it's just not possible what I want?我做错了什么或者我想要的是不可能的?

ELMAH always uses the base exception to generate the Type and Error fields. ELMAH 总是使用基本异常来生成类型和错误字段。 This is the best (IMO) way of doing it since the base exception will always be the original cause of the error.这是最好的(IMO)方法,因为基本异常将始终是错误的原始原因。 This explains why you get the type and message of the IndexOutOfRangeException logged in ELMAH.这解释了为什么您会在 ELMAH 中获得记录IndexOutOfRangeException的类型和消息。

There's a small "hack" to resolve this if you switch to using the Log method:如果您切换到使用Log方法,有一个小的“hack”可以解决这个问题:

try{
    try{
        throw new IndexOutOfRangeException();
    }
    catch (Exception e){
        throw new MyCustomException("Message to see in elmah 'Error' column", e)
    }
}
catch(MyCustomException cex){
    var error = new Elmah.Error(cex);
    // Type and Error will now have the values of `IndexOutOfRangeException`
    error.Type = cex.GetType().FullName;
    error.Message = cex.Message;
    // I manually updated the values from `MyCustomException`
    ErrorLog.GetDefault(HttpContext.Current).Log(error);
}
catch(Exception ex){
    // some code here
}

I tested this locally and both Type and Error get the value from the outer exception.我在本地对此进行了测试, TypeError都从外部异常中获取了值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM