Open sandboxFocusImprove this doc

Example: tracing calls to investigate a problem

A developer or an AI agent who investigates a problem without a debugger often adds log statements to many files, and must remove them after the investigation. With interceptors, you can trace the calls to a library from one file: a project fabric that registers a T# template for each call to the library. The source code of the calling code does not change. To remove the tracing, you delete the file.

This series shows how to trace the calls to InventoryClient, a class of a separate class library. Each call to its methods is rewritten to call a generated interceptor, which writes the caller, the arguments, the result or the exception, and the duration of the call:

Source Code
1using Metalama.Samples.CallTracing.Inventory;
2
3namespace Metalama.Samples.CallTracing1;
4
5public class OrderService
6{
7    private readonly InventoryClient _inventory = new();
8
9    public bool CanOrder( string sku, int quantity ) => this._inventory.GetStock( sku ) >= quantity;
10
11    public void PlaceOrder( string sku, int quantity )
12    {
13        this._inventory.Reserve( sku, quantity );
14    }
15}
16
Transformed Code
1using Metalama.Samples.CallTracing.Inventory;
2
3namespace Metalama.Samples.CallTracing1;
4
5public class OrderService
6{
7    private readonly InventoryClient _inventory = new();
8
9    public bool CanOrder( string sku, int quantity ) => MetalamaInterceptors.InventoryClient_GetStock_Interceptor( this._inventory,sku:sku ) >= quantity;
10
11    public void PlaceOrder( string sku, int quantity )
12    {
13        MetalamaInterceptors.InventoryClient_Reserve_Interceptor( this._inventory,sku:sku,quantity:quantity );
14    }
15}
16

In this series

Article Description
Call tracing example, step 1: Tracing calls to a library A project fabric and a T# template trace each call to the API of a class library.
Call tracing example, step 2: Enabling tracing only in a trace build The fabric registers the interceptors only when an MSBuild property is set.