Skip to content

Latest commit

 

History

History
91 lines (50 loc) · 3.41 KB

debugging-aspect-oriented-code.md

File metadata and controls

91 lines (50 loc) · 3.41 KB
uid level summary
debugging-aspect-oriented-code
100
The document provides a step-by-step guide on how to debug aspect-oriented code using the Visual Studio debugger, including setting up a debug configuration, using breakpoints and step-into, and forcefully breaking the program.

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 xref:preview-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

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.

Show_All_Files

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

[!metalama-test ~/code/DebugDemo/Program.cs]

Note

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

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

[!div class="see-also"] xref:debugging-aspects xref:video-debugging