c# - Does getting current class and method name reduce performance? -


i have logging class creates instance of log4net , can call anywhere in code. have method find out class , method name of caller. here method:

private static string currentmethod() {     stacktrace stacktrace = new stacktrace();     methodbase method = stacktrace.getframe(2).getmethod();     string cn = method.reflectedtype.name;     string mn = method.name;      string output = "[" + cn + "." + mn + "]";     return output; } 

i found these articles: link1, link2, link3, link4 , many others, none of them discuss efficiency , performance. have 2 questions: 1- can use method in big project(about 1000 requests in 1 second)? mean how effect project's efficiency , performance? 2- os there better way write above method?

also here part of logging class. guess can help:

public static class tools_log {     private static ilog logger;      public static ilog getlogger()     {         return logger ?? (logger = createlogger());     }     private static ilog createlogger()     {         //some log4net initialization         return logmanager.getlogger("webservice");     }      private static string currentmethod()     {         stacktrace stacktrace = new stacktrace();         methodbase method = stacktrace.getframe(2).getmethod();         string cn = method.reflectedtype.name;         string mn = method.name;          string output = "[" + cn + "." + mn + "]";         return output;     }      public static string messageforlogfile(string message, string exeption, double time)     {         string currentmethod;         string executiontime;         string consumer;         string body;         string output;          currentmethod = currentmethod();         executiontime = executiontime(time);         body = bodyforlog(message, exeption);         consumer = consumer();          output = output(currentmethod, executiontime, consumer, body);         return output;     } } 

i call log class this:

tools_log.getlogger().info(tools_log.messageforlogfile("some text", "some text", execution time)); 

thanks.

yes, reflection slower process. if want name of method can quite callermembernameattribute. add string parameter empty default value, , apply attribute it, , compiler send name you.

also, if you're looking fast logging, take @ etw instead of log4net.

here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb968803(v=vs.85).aspx

and here: http://blogs.msdn.com/b/vancem/archive/2012/07/09/logging-your-own-etw-events-in-c-system-diagnostics-tracing-eventsource.aspx

edit:
question in comments, unfortunately, can't name of method 2 levels up, @ least, not directly. callermembername syntax sugar, tells compiler take name of calling member, , place in parameter, works on single level. however, since relies on default parameters so, if send parameter manually, callermembername functionality doesn't apply, can this:

public void usermethod() {     intermediarymethod(); }  public void intermediarymethod([callermembername] caller = "") {     logmethod(caller) }  public void logmethod([callermembername] caller = "") {     // log... } 

if pass value yourself, won't overridden, doing allow name of caller 2 levels up, while retaining functionality single level.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -