Efficient way to record time spent executing a method in C #

I am looking for a simple, reusable way to record the time taken to execute a method in my .NET project (MVC) and write it using NLog and was wondering if anyone has any suggestions on how to implement this?

To maintain clean code and readability, I really don't want to integrate it into my code, if possible.

If anyone has any suggestions, I'd love to hear them.

+5
source share
3 answers

Maybe you should take a look at this:

http://code.google.com/p/mvc-mini-profiler/

.

+3

Stopwatch

Stopwatch time = Stopwatch.StartNew();
time.Stop();
var milliseconds= time.ElapsedMilliseconds;
+10

Since you mention that you want to avoid modifying existing code, this is the case when AOP (aspect-oriented programming) comes in handy

Here's a blog post that describes this particular scenario using PostSharp: http://theburningmonk.com/2010/03/aop-method-execution-time-watcher-with-postsharp/

+5
source

All Articles