ProfK
ProfK

Reputation: 51124

Postsharp OnException Aspect not Working as Expected

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]

Upvotes: 5

Views: 1553

Answers (2)

Arkadas Kilic
Arkadas Kilic

Reputation: 2484

If Postsharp licence is not valid or expired, It's skipping OnException method in your class. Updating Postsharp with a valid licence number solved my issue.

Upvotes: 0

AFract
AFract

Reputation: 9700

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.

Upvotes: 3

Related Questions