Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MethodBase.GetCurrentMethod() Performance?

I have written a log class and a function as in the following code:

Log(System.Reflection.MethodBase methodBase, string message)

Every time I log something I also log the class name from the methodBase.Name and methodBase.DeclaringType.Name.

I read the following post Using Get CurrentMethod and I noticed that this method is slow.

Should I use the this.GetType() instead of System.Reflection.MethodBase or I should manually log the class/method name in my log e.g. Log("ClassName.MethodName", "log message)? What is the best practice?

like image 201
Ioannis Avatar asked Feb 03 '11 11:02

Ioannis


1 Answers

It really depends.

If you use the this.GetType() approach you will lose the method information, but you will have a big performance gain (apparently a factor of 1200, according to your link).

If you offer an interface that lets the caller supply strings (e.g. Log("ClassName.MethodName", "log message"), you will probably gain even better performance, but this makes your API less friendly (the calling developer has to supply the class name and method name).

like image 137
Edwin de Koning Avatar answered Oct 14 '22 08:10

Edwin de Koning