static void PrintTimers() { //Here we are printing the data but it also can be saved into a persistent storage var timers = MethodExecutionListSingleton.GetTimers(); foreach (var methodExecution in timers.listOfMethodExecutions) { Console.WriteLine($"Method name:{methodExecution.MethodName}, " + $"Execution time: {methodExecution.ExecutionTime}," + $"Start time: {methodExecution.StartTime:hh.mm.ss.ffffff}," + $"End time: {methodExecution.EndTime:hh.mm.ss.ffffff}"); } }
public override object Advise(IInvocation invocation) { // do something before target method is Called // ... // Console.WriteLine($"Entering {invocation.Method.Name}()"); Timer = new Stopwatch(); Timer.Start(); var mExecution = new MethodExecution(); mExecution.MethodName = invocation.Method.Name; mExecution.StartTime = DateTime.Now; try { return(invocation.Proceed()); // call the next advice in the "chain" of advice pipeline, or call target method } catch (Exception e) { // do something when target method throws exception // ... Console.WriteLine($"TimeIt catches an exception: {e.Message}"); throw; } finally { // do something after target method is Called // ... Timer.Stop(); // Console.WriteLine($"Leaving {invocation.Method.Name}()"); // Console.WriteLine($"Time taken by method {invocation.Method.Name}() is {Timer.ElapsedMilliseconds} ms."); mExecution.EndTime = DateTime.Now; mExecution.ExecutionTime = Timer.ElapsedMilliseconds; var timers = MethodExecutionListSingleton.GetTimers(); timers.listOfMethodExecutions.Add(mExecution); } }