MethodBoundaryAspect.Fody 3.7.0
MethodBoundaryAspect.Fody
Run your own code when a method starts, ends or throws, by putting an attribute on it:
[Log]
public int Add(int a, int b) => a + b;
MethodBoundaryAspect.Fody is a Fody weaver: the calls to your aspect are woven into the IL of the method at build time. There is no runtime proxy, no dependency injection container and no interface or virtual method required. It works for static, private, generic and async methods of any class, on .NET Framework 4.6.2+ and .NET (Core) / .NET Standard 2.0+.
Typical aspects: logging, measuring execution time, transaction handling, exception wrapping, showing a wait cursor, argument validation, caching.
Quickstart
Add this package (it brings Fody along).
Add a
FodyWeavers.xmlfile to the project root:<Weavers> <MethodBoundaryAspect /> </Weavers>Write an aspect by deriving from
OnMethodBoundaryAspectand overriding any ofOnEntry,OnExitandOnException:using System; using MethodBoundaryAspect.Fody.Attributes; public sealed class LogAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) => Console.WriteLine($"Entering {args.Method.Name}({string.Join(", ", args.Arguments)})"); public override void OnExit(MethodExecutionArgs args) => Console.WriteLine($"{args.Method.Name} returned {args.ReturnValue}"); public override void OnException(MethodExecutionArgs args) => Console.WriteLine($"{args.Method.Name} failed: {args.Exception.Message}"); }Apply it to a method, a class, a property or the whole assembly (
[assembly: Log]) and build:public class Calculator { [Log] public int Add(int a, int b) => a + b; } new Calculator().Add(1, 2); // Entering Add(1, 2) // Add returned 3
What an aspect can do
All three aspect methods get a MethodExecutionArgs with the information of the call:
| Property | Content |
|---|---|
Instance |
the object the method is called on, null for static methods |
Method |
the called method as MethodBase |
Arguments |
the argument values (can be changed with [AllowChangingInputArguments] on the aspect) |
ReturnValue |
the return value in OnExit, can be overwritten |
Exception |
the thrown exception in OnException |
FlowBehavior |
Return in OnEntry skips the method body, Continue in OnException swallows the exception |
MethodExecutionTag |
any object you want to pass from OnEntry to OnExit/OnException of the same call |
- Select the methods with
[DisableWeaving], regular expression filters for namespace, type and method name, and the visibility of the methods (AttributeTargetMemberAttributes). - Combine aspects on one method and order them with
[AspectOrderIndex]. - Async methods are supported:
OnEntryruns when the method is called,OnExitwhen it returns itsTask(args.ReturnValueis that task, the work may not be finished),OnExceptionwhen the asynchronous work fails. - Low overhead: the weaver only provides the
MethodExecutionArgsproperties your aspects use, and creates aspects without state once instead of for every call.
Documentation
The complete documentation is the README on GitHub and the pages in its docs folder:
- How it works, Writing aspects and Applying aspects
- Changing the method behavior (return values, arguments, skipping the method, swallowing exceptions) and Async methods
- Limitations, Performance and the Configuration reference
- Troubleshooting (aspect not called, fields reset,
nullproperties) - Upgrading to version 3 and the Changelog
- Complete sample projects: .NET and .NET Framework
Questions and bug reports: GitHub issues. MIT license.
Showing the top 20 packages that depend on MethodBoundaryAspect.Fody.
| Packages | Downloads |
|---|---|
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
18 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
16 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
15 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
14 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
13 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
12 |
|
TensorFlow.NET
Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io
|
10 |
| Version | Downloads | Last updated |
|---|---|---|
| 3.7.0 | 1 | 11/20/2026 |
| 3.6.0 | 6 | 11/03/2026 |
| 3.5.0 | 7 | 11/02/2026 |
| 3.4.0 | 7 | 11/02/2026 |
| 3.3.0 | 6 | 11/02/2026 |
| 3.2.1 | 5 | 10/20/2026 |
| 3.2.0 | 8 | 10/22/2026 |
| 3.1.0 | 6 | 10/27/2026 |
| 3.0.0 | 9 | 10/23/2026 |
| 2.1.0 | 5 | 10/27/2026 |
| 2.0.152 | 5 | 11/03/2026 |
| 2.0.151 | 8 | 10/23/2026 |
| 2.0.150 | 9 | 01/02/2026 |
| 2.0.149 | 10 | 01/03/2026 |
| 2.0.149-tags-v2-0-149-pre.1 | 13 | 01/03/2026 |
| 2.0.148 | 12 | 12/10/2025 |
| 2.0.147-pre | 9 | 01/03/2026 |
| 2.0.146 | 10 | 12/10/2025 |
| 2.0.146-pre | 40 | 12/11/2025 |
| 2.0.145 | 8 | 01/03/2026 |
| 2.0.144 | 9 | 01/03/2026 |
| 2.0.143 | 8 | 01/03/2026 |
| 2.0.142 | 12 | 12/11/2025 |
| 2.0.141 | 6 | 12/11/2025 |
| 2.0.140 | 9 | 12/10/2025 |
| 2.0.139 | 10 | 12/09/2025 |
| 2.0.138 | 10 | 01/03/2026 |
| 2.0.137 | 10 | 12/11/2025 |
| 2.0.136 | 8 | 01/03/2026 |
| 2.0.135 | 9 | 12/10/2025 |
| 2.0.134 | 13 | 12/11/2025 |
| 2.0.133 | 7 | 12/10/2025 |
| 2.0.132 | 10 | 12/11/2025 |
| 2.0.131 | 9 | 01/03/2026 |
| 2.0.130 | 11 | 01/03/2026 |
| 2.0.129 | 9 | 01/03/2026 |
| 2.0.128 | 15 | 01/03/2026 |
| 2.0.123 | 6 | 12/09/2025 |
| 2.0.122 | 9 | 12/11/2025 |
| 2.0.118 | 12 | 01/03/2026 |
| 2.0.115 | 14 | 12/10/2025 |
| 2.0.113 | 9 | 01/03/2026 |
| 2.0.111 | 11 | 12/09/2025 |
| 2.0.107 | 9 | 01/04/2026 |
| 1.0.95 | 7 | 01/03/2026 |
| 1.0.86 | 9 | 01/03/2026 |
| 1.0.74 | 7 | 01/03/2026 |
| 1.0.66 | 8 | 01/03/2026 |
| 1.0.64 | 9 | 12/09/2025 |
| 1.0.62 | 6 | 01/03/2026 |
| 1.0.60 | 10 | 12/11/2025 |
| 1.0.58 | 11 | 01/03/2026 |
| 1.0.56 | 11 | 01/03/2026 |
| 1.0.54 | 9 | 12/11/2025 |
| 1.0.53 | 10 | 01/03/2026 |
| 1.0.51 | 11 | 01/03/2026 |
| 1.0.49 | 11 | 12/11/2025 |
| 1.0.41 | 15 | 01/03/2026 |
| 1.0.39 | 12 | 12/10/2025 |
| 1.0.36 | 10 | 12/09/2025 |
| 1.0.23 | 10 | 01/03/2026 |
| 1.0.16 | 13 | 01/03/2026 |
| 1.0.14 | 12 | 01/03/2026 |
| 1.0.12 | 8 | 01/03/2026 |
| 1.0.11 | 10 | 01/03/2026 |
| 1.0.10 | 9 | 01/03/2026 |
| 1.0.9 | 4 | 12/29/2025 |
| 1.0.8 | 10 | 12/09/2025 |
| 1.0.7 | 15 | 12/09/2025 |
| 1.0.3 | 11 | 12/09/2025 |
| 1.0.2 | 10 | 12/09/2025 |
| 0.3.50 | 8 | 01/03/2026 |
| 0.3.44 | 12 | 12/11/2025 |
| 0.3.43 | 16 | 12/09/2025 |
| 0.3.42 | 11 | 12/11/2025 |
| 0.3.41 | 10 | 01/03/2026 |
| 0.3.39 | 9 | 01/03/2026 |
| 0.3.38 | 10 | 12/10/2025 |
| 0.3.37 | 8 | 01/03/2026 |
| 0.3.36 | 10 | 01/03/2026 |
| 0.3.35 | 10 | 12/11/2025 |
| 0.3.33 | 13 | 12/11/2025 |
| 0.3.31 | 13 | 12/10/2025 |
| 0.3.29 | 13 | 01/03/2026 |
| 0.3.28 | 11 | 12/10/2025 |
| 0.3.27 | 8 | 12/09/2025 |
| 0.3.24 | 10 | 01/03/2026 |
| 0.3.23 | 9 | 01/03/2026 |
| 0.3.22 | 11 | 12/11/2025 |
| 0.3.21 | 9 | 01/03/2026 |
| 0.3.19 | 11 | 12/11/2025 |
| 0.3.18 | 8 | 01/03/2026 |
| 0.3.15 | 10 | 01/03/2026 |
| 0.3.14 | 9 | 01/03/2026 |
| 0.3.13 | 12 | 12/09/2025 |
| 0.3.12 | 11 | 12/11/2025 |
| 0.3.11 | 9 | 12/11/2025 |
| 0.3.10 | 13 | 12/09/2025 |
| 0.3.5 | 17 | 12/11/2025 |