5

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]
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • The GetExceptionType override is redundant, as it will catch `Exception` anyway. Show us how you decorated your class / project – Yuval Itzchakov May 21 '14 at 11:10
  • @YuvalItzchakov, please see modified question. – ProfK May 21 '14 at 11:22
  • Try `[assembly: YourNamespace.AutoLogExceptionsAspect(AttributeTargetTypes = "YourNamespace.*")]` – Yuval Itzchakov May 21 '14 at 11:32
  • No difference I'm afraid. – ProfK May 21 '14 at 12:52
  • When you say "intentional divide by zero", you do mean integer divide by zero, correct? – Joel Rondeau May 23 '14 at 13:16
  • Correct. It now picks it up though, something came right with the PostSharp role in the build process. – ProfK May 24 '14 at 06:58
  • Are you sure that your assembly is 'postharped'? Make sure your aspect is in fact weaved in. Take some reflector tool, dissasemble your compiled dll and check if the postsharp code is there. You could use ILSpy for example. – Edin May 26 '14 at 15:06
  • Are you missing this line: base.OnException(args); at the end of your public override void OnException() method? – Only You May 29 '14 at 19:20

2 Answers2

3

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.

AFract
  • 8,868
  • 6
  • 48
  • 70
  • It somehow came right itself, but it was a build time issue. Suddenly one day PostSharp produced a build error executing one of its tools, and that highlighted a seemingly unrelated problem which I fixed. – ProfK May 30 '14 at 12:34
0

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.

Arkadas Kilic
  • 2,416
  • 2
  • 20
  • 16