### Asynchronous Method Sample Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md An example of an asynchronous method decorated with a Log attribute. This demonstrates the basic structure for applying aspects to async methods. ```csharp using static System.Console; public class Sample { [Log] public async Task MethodAsync() { WriteLine("Entering original method"); await OtherClass.DoSomeOtherWorkAsync(); WriteLine("Exiting original method"); } } ``` -------------------------------- ### Implement Log Aspect Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Example of creating a custom logging aspect by deriving from OnMethodBoundaryAspect. It logs messages on method entry, exit, and exception. ```csharp using static System.Console; using MethodBoundaryAspect.Fody.Attributes; public sealed class LogAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { WriteLine("On entry"); } public override void OnExit(MethodExecutionArgs args) { WriteLine("On exit"); } public override void OnException(MethodExecutionArgs args) { WriteLine("On exception"); } } ``` -------------------------------- ### Implement TransactionScope Aspect Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Example of creating a custom aspect for transaction handling by deriving from OnMethodBoundaryAspect. It manages a TransactionScope instance using the MethodExecutionTag. ```csharp using MethodBoundaryAspect.Fody.Attributes; public sealed class TransactionScopeAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { args.MethodExecutionTag = new TransactionScope(); } public override void OnExit(MethodExecutionArgs args) { var transactionScope = (TransactionScope)args.MethodExecutionTag; transactionScope.Complete(); transactionScope.Dispose(); } public override void OnException(MethodExecutionArgs args) { var transactionScope = (TransactionScope)args.MethodExecutionTag; transactionScope.Dispose(); } } ``` -------------------------------- ### Expected Output for Log Aspect (Success) Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Illustrates the expected console output when the Log aspect is applied to a method that executes successfully without exceptions. ```text On entry Entering original method Exiting original method On exit ``` -------------------------------- ### Expected Output for Log Aspect (Exception) Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Illustrates the expected console output when the Log aspect is applied to a method that throws an exception. Note that OnExit is not called in this case. ```text On entry Entering original method On exception ``` -------------------------------- ### Apply Log Aspect to a Method Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Shows how to apply the custom Log aspect to a method. This will log the execution flow, including entry and exit points, and exceptions. ```csharp using static System.Console; public class Sample { [Log] public void Method() { WriteLine("Entering original method"); OtherClass.DoSomeOtherWork(); WriteLine("Exiting original method"); } } ``` -------------------------------- ### Apply TransactionScope Aspect to a Method Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Demonstrates how to apply the custom TransactionScope aspect to a method. This will wrap the method's execution within a transaction. ```csharp public class Sample { [TransactionScope] public void Method() { Debug.WriteLine("Do some database stuff isolated in surrounding transaction"); } } ``` -------------------------------- ### Log Aspect for Asynchronous Methods with ContinueWith Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md A custom Log aspect for asynchronous methods that uses ContinueWith on the returned Task to ensure the 'On exit' logic executes after the asynchronous operation completes. This is useful when the OnExit handler needs to wait for the async work to finish. ```csharp using static System.Console; using MethodBoundaryAspect.Fody.Attributes; public sealed class LogAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { WriteLine("On entry"); } public override void OnExit(MethodExecutionArgs args) { if (args.ReturnValue is Task t) t.ContinueWith(task => WriteLine("On exit")); } public override void OnException(MethodExecutionArgs args) { WriteLine("On exception"); } } ``` -------------------------------- ### Handle Exceptions in Async Methods Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Modify the return value of an async method in its OnExit handler to include custom exception handling. This allows for processing task results or exceptions. ```csharp using MethodBoundaryAspect.Fody.Attributes; using System; using System.Linq; using static System.Console; public sealed class HandleExceptionAttribute : OnMethodBoundaryAspect { public override void OnExit(MethodExecutionArgs args) { if (args.ReturnValue is Task task) { args.ReturnValue = task.ContinueWith(t => { if (t.IsFaulted) return "An error happened: " + t.Exception.Message; return t.Result; }); } } } public class Program { [HandleException] public static async Task Process() { await Task.Delay(10); throw new Exception("Bad data"); } public static async Task Main(string[] args) { WriteLine(await Process()); // Output: "An error happened: Bad data" } } ``` -------------------------------- ### Log Aspect for Asynchronous Methods with Tagging Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md A custom Log aspect designed for asynchronous methods that uses MethodExecutionTag to prevent 'On exception' from being called if 'On exit' has already run. This ensures correct exception handling in scenarios where OnExit might precede OnException. ```csharp using static System.Console; using MethodBoundaryAspect.Fody.Attributes; public sealed class LogAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { WriteLine("On entry"); arg.MethodExecutionTag = false; } public override void OnExit(MethodExecutionArgs args) { WriteLine("On exit"); arg.MethodExecutionTag = true; } public override void OnException(MethodExecutionArgs args) { if ((bool)arg.MethodExecutionTag) return; WriteLine("On exception"); } } ``` -------------------------------- ### Change Method Input Arguments Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Hook into the OnEntry handler to modify input arguments by accessing the Arguments property of MethodExecutionArgs. Ensure the aspect is annotated with AllowChangingInputArgumentsAttribute. ```csharp using System; using MethodBoundaryAspect.Fody.Attributes; [AllowChangingInputArguments] public sealed class InputArgumentIncrementorAttribute : OnMethodBoundaryAspect { public int Increment { get; set; } public override void OnEntry(MethodExecutionArgs args) { var inputArguments = args.Arguments; for (var i = 0; i < inputArguments.Length; i++) { var value = inputArguments[i]; if (value is int v) inputArguments[i] = v + Increment; } } } public class Program { public static void Main(string[] args) { // ByValue MethodByValue(10); // ByRef var value = 10; MethodByRef(ref value); Console.WriteLine("after method call: " + value); // Output: 20 } [InputArgumentIncrementor(Increment = 1)] public static void MethodByValue(int i) { Console.WriteLine(i); // Output: 11 } [InputArgumentIncrementor(Increment = 10)] public static void MethodByRef(ref int i) { Console.WriteLine(i); // Output: 20 } } ``` -------------------------------- ### Change Method Return Value Source: https://github.com/vescon/methodboundaryaspect.fody/blob/master/README.md Hook into the OnExit handler to set the ReturnValue property for modifying a method's output. This is applicable to both synchronous and asynchronous methods. ```csharp using MethodBoundaryAspect.Fody.Attributes; using System; using System.Linq; using static System.Console; public sealed class IndentAttribute : OnMethodBoundaryAspect { public override void OnExit(MethodExecutionArgs args) { args.ReturnValue = String.Concat(args.ReturnValue.ToString().Split('\n').Select(line => " " + line)); } } public class Program { [Indent] public static string GetLogs() => @"Detailed Log 1 Detailed Log 2"; public static void Main(string[] args) { WriteLine(GetLogs()); // Output: " Detailed Log 1\n Detailed Log 2"; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.