Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postsharp OnException Aspect not Working as Expected

Tags:

c#

.net

postsharp

I have the following custom aspect, and have tried applying it at project and class level. In all cases, even an intentional divide by zero, the OnException method is never called. What am I doing wrong?

[Serializable]
public class AutoLogExceptionsAspect : OnExceptionAspect
{
  public override void OnException(MethodExecutionArgs args)
  {
    AutoLogExceptionEventSource.Log.AutoLogException(args.Exception.GetType().Name, args.Exception.Message, args.Exception.StackTrace);
    args.FlowBehavior = FlowBehavior.Continue;
  }

  public override Type GetExceptionType(MethodBase targetMethod)
  {
    return typeof(Exception);
  }
}

I have tried this decoration on a class:

[AutoLogExceptionsAspect]
public partial class App : Application

and this one on the project:

[assembly: AutoLogExceptionsAspect]
like image 961
ProfK Avatar asked Oct 31 '22 23:10

ProfK


1 Answers

IMHO AutoLogExceptionsAspect is not related to onexception aspect (or at least is not mandatory).

Usually when Exception (or other "methods" aspects attributes) are not called, it is because there was a problem in build chain at PostSharp call time.

Please get sure if Postsharp is up and running on build machines, and Postsharp is enabled in project properties (for example, no "SkipPostSharp" switch in properties on assemblies which needs PostSharp to be processed). If not attributes won't be executed.

like image 164
AFract Avatar answered Nov 15 '22 04:11

AFract