MetalamaConceptual documentationUsing MetalamaDebugging aspect oriented code
Open sandboxFocusImprove this doc

Debugging aspect-oriented code

In this section, we illustrate how to debug aspect-oriented code using the Visual Studio debugger. This builds on our previous discussion about how aspects transform your code, which included a side-by-side comparison of the original and transformed code at Metalama Diff.

Steps to debug aspect-oriented code

To debug your code with aspects, follow these steps:

Step 1: Open Build Configuration Manager

From the debug setting dropdown, select Configuration Manager as depicted below:

Step 2: Create a debug configuration named LamaDebug

The Configuration Manager will present the following dialog:

  • Open the Active solution configuration dropdown.

  • Click on <New...> to create a new debug configuration. This action will open the New dialog as depicted below.

  • Enter the name LamaDebug and copy settings from Debug as shown below.

  • Save this configuration by clicking the OK button.
  • Change the build configuration to LamaDebug.

You are now prepared to debug your aspect-transformed code.

Breakpoints and Step-Into

If you set a breakpoint in your code that is being modified by an aspect, those breakpoints will not be hit. However, you can use F11 to Step-Into as usual.

You can also set breakpoints in the transformed code. In the following sections, we will guide you on how to locate the transformed code and how to debug it.

Consider the following code with the logging ([Log]) aspect:

The logging aspect adds a line at the beginning of the method it intercepts, indicating the name of the intercepted method. When you step into this code by pressing F11, you should see the transformed code as shown.


Note that the line Console.WriteLine("Running Demo.DoThis()") is generated by the Logging aspect in the AspectLib namespace.

To locate the transformed code, click on the Show all files button as shown below.


Once all files in your solution explorer are displayed, locate the file under LamaDebug\net6.0\metalama as shown in the solution explorer screenshot below.

As demonstrated, you can set a breakpoint on this transformed code, and it will be hit because this is the compiled code.

Debugging using step-in and forceful break

Source Code
1using Metalama.Documentation.QuickStart;
2using System.Diagnostics;

4namespace DebugDemo
6    public class Demo
7    {
8        public static void Main()
9        {
10            Debugger.Break();
11            DoThis();

12        }

14        [Log]
15        public static void DoThis()
16        {
17            Console.WriteLine( "Doing this" );
18            DoThat();
19        }


21        [Log]
22        public static void DoThat()
23        {
24            Console.WriteLine( "Doing that" );
25        }
26    }

Transformed Code
1using Metalama.Documentation.QuickStart;
2using System;
3using System.Diagnostics;
5namespace DebugDemo
7    public class Demo
8    {
9        public static void Main()
10        {
11Console.WriteLine("Executing Demo.Main().");
12            Debugger.Break();
13            DoThis();
14            return;
15        }
17        [Log]
18        public static void DoThis()
19        {
20Console.WriteLine("Executing Demo.DoThis().");
21            Console.WriteLine("Doing this");
22            DoThat();
23            return;
24        }
26        [Log]
27        public static void DoThat()
28        {
29Console.WriteLine("Executing Demo.DoThat().");
30            Console.WriteLine("Doing that");
31            return;
32        }
33    }

When you debug this code by Step-Into, you will observe that the actual code being debugged is the transformed code.

Breaking forcefully using Debugger.Break

You can use Debugger.Break to forcefully break the program. The following screenshot illustrates its usage.


Note that it's the same code you saw before. You can add Debugger.Break to forcefully break the debugger at that location.