开发者

PostSharp handle handled exceptions

Ther开发者_如何学编程e are many Try/Catch blocks in my app to catch exceptions. I would like to read such handled exceptions and log them to a file. Is it possible to read handled exceptions with PostSharp?


no. PostSharp works by wrapping your methods in try/catch blocks of its own and then just rethrowing the exception. Any exceptions handled in your method would be an inner try/catch while postsharp would only have outer try/catch blocks. You would either 1) have to rethrow the exception or 2) Handle those exceptions using an aspect. Neither of which I recommend.


One way to handle this (!) is to have a method that you call within the catch that will log the parameters passed into the exception. Simply pass the exception in and the logger will log the information.

[LogParameters(LogLevel.Error)]
private static void Error(Exception ex) { }

public class LogParameters : OnMethodBoundaryAspect {
    public override void OnEntry(MethodExcutionArgs args) {
        for (int i=0; i<args.Arguments.Count; i++) {
            // Get argument from args.Arguments.GetArgument(i)
        }
    }
}

Using the OnEntry method of a customized OnMethodBoundaryAspect, you can log the exception information by calling a method and passing in the exception. The method doesn't need to actually DO anything, it just is a dummy for the aspect to wrap around and log the exception parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜