It may seem unusual to debug compile-time logic, but like any process, it is perfectly legal and even simple to debug the build process!

Basically, what you will do is to attach a debugger to the PostSharp process. If you use the standard MSBuild targets for PostSharp, define the constant PostSharpAttachDebugger=True.

Attaching the debugger: an easy recipe

The trick is easier to explain when you have compile-time logic (your PostSharp plug-in or Laos aspect, for instance) and the transformed assembly in different Visual Studio projects.

Suppose you have a your compile-time logic MyWeaver.csproj and unit tests of the weaver (i.e. the code to be transformed) in MyWeaver.Test.csproj. The easiest way to debug MyWeaver.csproj is to:

  1. Open MyWeaver.csproj and MyWeaver.Test.csproj in two different instances of Visual Studio.
  2. Open the SDK or Visual Studio Command Prompt and go to the directory containing MyWeaver.Test.csproj.
  3. Build MyWeaver.csproj using Visual Studio.
  4. From the command prompt, type:
    msbuild MyWeaver.Test.csproj /T:Rebuild /P:PostSharpAttachDebugger=True
    
  5. The build process will hit a break point. When it happens, attach the instance of MyWeaver.csproj Visual Studio. Set up break points in your code and continue the program execution.

Getting PostSharp debugging symbol

We provide builds of PostSharp in debugging mode. You can download a debug build of PostSharp on one side, and the source code on the other side, to be able to step into PostSharp.

Tracing the execution of compile-time logic

Tracing often makes it easier to understand what happens in your program. This is also true with compile-time logic.

Enabling tracing

Enabling tracing is useful when you need to understand better the behavior of PostSharp or when you need to trace your own code.

The tracing facility is totally disabled in the release build. You need first to download a debugbuild.

You should then edit the file PostSharp-Library.config and enable tracing for individual categories.

Finally, you should enable tracing when you invoke PostSharp. Invoke MSBuild from the command line and specify the parameters:

msbuild /p:PostSharpVerbose=true /v:detailed

Emitting tracing information

The easiest way to generate tracing information is to use the Console.WriteLine method. If your tracing requirements are very limited, it is maybe the best solution because it's so simple.

If you have complex needs, it is appropriate to use the PostSharp tracing facility.

The design pattern to create a tracing category is to define a static class, for instance MyTrace with a singleton instance initialized to a new instance of the Trace class.

internal static class MyTrace
{
  public static readonly Trace Instance = new Trace("MyTrace");
} 

The last step is to enable the category in PostSharp-Library.config.