[Serializable] public class MyAspect: OnExceptionAspect { public override void OnException(MethodExecutionArgs args) { throw new MyCustomException(args.Exception); } } public class MyCustomException : Exception { public override string StackTrace { get {
You really need to throw a new exception. The @Ani example will simply rebuild the exception that was already sent with the same stack trace (this is the same because of what you got in this aspect). Throwing a new exception will "modify" the stack trace, but will not delete it. If you want to remove it, you will need to throw your own class, which overrides the stack trace property. passing the old exception to the new exception will make the old exception an internal exception (if you want it)
You can accomplish this with and without PostSharp. The key is your own exception class.
Given the following code
class Program { static void Main(string[] args) { try { Test1(); } catch (Exception e) { Console.WriteLine(e.StackTrace + Environment.NewLine); } Console.ReadKey(); } private static void Test1() { try { Test2(); } catch (Exception e) { Console.WriteLine(e.StackTrace + Environment.NewLine); throw e; } } private static void Test2() { try { Test3(); } catch (Exception e) { Console.WriteLine(e.StackTrace + Environment.NewLine); throw; } } [MyAspect] private static void Test3() { throw new InvalidOperationException(); } } [Serializable] public class MyAspect : OnExceptionAspect { public override void OnException(MethodExecutionArgs args) { throw args.Exception; } }
conclusion
in ConsoleApplication5.MyAspect.OnException (MethodExecutionArgs args) in C: \ T est \ Program.cs: line 69 at ConsoleApplication5.Program.Test3 () in C: \ Test \ Program.cs: line 59
in ConsoleApplication5.Program.Test2 () in C: \ Test \ Program.cs: line 47
in ConsoleApplication5.MyAspect.OnException (MethodExecutionArgs args) in C: \ T est \ Program.cs: line 69 at ConsoleApplication5.Program.Test3 () in C: \ Test \ Program.cs: line 59
in ConsoleApplication5.Program.Test2 () in C: \ Test \ Program.cs: line 52
in ConsoleApplication5.Program.Test1 () in C: \ Test \ Program.cs: line 34
in ConsoleApplication5.Program.Test1 () in C: \ Test \ Program.cs: line 39 in ConsoleApplication5.Program.Main (String [] args) in C: \ Test \ Program.cs: line 19
source share