Skip to main content
Version: Next

IL Weaving Enhancement Mechanism

1. Introduction to Mechanism​

The system uses Fody and MethodTimer.Fody to implement IL (Intermediate Language) weaving enhancement at compile time. This mechanism allows injecting specific logic (such as performance monitoring, logging, property notification, etc.) during the compilation phase without modifying the source code, keeping the business code clean.

2. Core Features​

2.1 Performance Monitoring (MethodTimer)​

  • Function: Automatically injects timing code into methods marked with specific attributes to record method execution time.
  • Application Scenario: Used for performance analysis of Application Service methods, Repository methods, etc., to help identify performance bottlenecks.
  • Configuration:
    • Configured in FodyWeavers.xml.
    • Use the [Time] attribute to mark methods or classes.
    • The MethodTimeLogger class handles the output of timing logs.

2.2 Property Notification (PropertyChanged - Optional)​

  • Function: Automatically implements the INotifyPropertyChanged interface, injecting property change notification code into property setters.
  • Application Scenario: Typically used in ViewModel scenarios in desktop clients (WPF/WinForms) or scenarios requiring change tracking. (Less commonly used in backend services, but supported).

3. Configuration and Implementation​

3.1 FodyWeavers.xml Configuration​

Located in the project root or specific class library directory, configuring the enabled Weavers.

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<MethodTimer />
<!-- Other Weavers can be added as needed -->
</Weavers>

3.2 MethodTimeLogger Implementation​

The system must define a MethodTimeLogger class to receive performance data intercepted by MethodTimer.

using System;
using System.Reflection;
using Microsoft.Extensions.Logging;

public static class MethodTimeLogger
{
public static void Log(MethodBase methodBase, long milliseconds, string message)
{
// Integrate with the system's Logging mechanism
// Note: Since this is a static method call, you may need a mechanism to access the ILogger instance (e.g., via a ServiceLocator or static generic logger)

// Example output
string logMessage = $"[Performance] Method: {methodBase.Name}, Duration: {milliseconds}ms, Message: {message}";
System.Diagnostics.Debug.WriteLine(logMessage);

// In a real production environment, write to structured logs (Serilog, etc.)
}
}

3.3 Usage Example​

using MethodTimer;

public class ProductAppService : ApplicationService
{
[Time("Querying Product List")]
public async Task<List<ProductDto>> GetProductsAsync()
{
// Business logic...
return await _repository.GetListAsync();
}
}

4. Advantages​

  1. Zero Intrusion: No need to manually write Stopwatch timing codes in each method.
  2. Compile-time Processing: IL modification is completed at compile time, with almost no runtime performance overhead (compared to dynamic proxies or AOP frameworks).
  3. Clean Code: Business logic focuses on business, separating cross-cutting concerns.

5. Considerations​

  • Debugging: Since the code is modified at compile time, the execution logic during debugging may differ slightly from the source code (e.g., injected code execution).
  • Build Time: The Weaving process will slightly increase the project compilation time.
  • Version Compatibility: Pay attention to the compatibility between the Fody version and the .NET SDK version.