MetalamaCommented examplesLoggingStep 6.​ Adding logging to many methods
Open sandboxFocusImprove this doc

Logging example, step 6: Adding the aspect programmatically

In the previous examples, we applied the logging aspect manually using the [Log] custom attribute. But when we must add logging to all methods of a namespace, doing it manually becomes tedious.

We can programmatically filter the code model of our project and add aspects to desired methods by adding a ProjectFabric to our code. Fabrics are compile-time classes executed by Metalama during compilation or from our IDE. They can add aspects to any eligible target declaration. For details, see Adding many aspects simultaneously.

The following code adds the logging aspect to all public methods of public types:

1using Metalama.Framework.Fabrics;
2using Metalama.Framework.Code;
3
4internal class Fabric : ProjectFabric
5{
6    public override void AmendProject( IProjectAmender amender ) =>
7        amender.Outbound
8            .SelectMany( compilation => compilation.AllTypes )
9            .Where( type => type.Accessibility == Accessibility.Public )
10            .SelectMany( type => type.Methods )
11            .Where( method => method.Accessibility == Accessibility.Public && method.Name != "ToString" )
12            .AddAspectIfEligible<LogAttribute>();
13}
Warning

It is important to exclude the ToString method from logging; otherwise, infinite recursion could occur.

We can see that the Calculator class has been transformed, even though there is no longer any custom attribute:

Source Code



1public class Calculator
2{














3    public double Add( double a, double b ) => a + b;


















4}
Transformed Code
1using System;
2using Microsoft.Extensions.Logging;
3
4public class Calculator
5{
6    public double Add( double a, double b )
7{
8        var isTracingEnabled = this._logger.IsEnabled(LogLevel.Trace);
9        if (isTracingEnabled)
10        {
11            LoggerExtensions.LogTrace(this._logger, $"Calculator.Add(a = {{{a}}}, b = {{{b}}}) started.");
12        }
13
14        try
15        {
16            double result;
17            result = a + b;
18            if (isTracingEnabled)
19            {
20                LoggerExtensions.LogTrace(this._logger, $"Calculator.Add(a = {{{a}}}, b = {{{b}}}) returned {result}.");
21            }
22
23            return (double)result;
24        }
25        catch (Exception e) when (this._logger.IsEnabled(LogLevel.Warning))
26        {
27            LoggerExtensions.LogWarning(this._logger, $"Calculator.Add(a = {{{a}}}, b = {{{b}}}) failed: {e.Message}");
28            throw;
29        }
30    }
31
32    private ILogger _logger;
33
34    public Calculator
35    (ILogger<Calculator> logger = default)
36    {
37        this._logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
38    }
39}
Warning

Including sensitive information (such as user credentials or personal data) in logs can pose a security risk. You should be cautious when adding parameter values to logs and avoid exposing sensitive data. To remove sensitive information from the logs, see Logging example, step 7: Removing sensitive data.