Using the StackTrace Class in a production environment to get calling method info

Posted by andy on Stack Overflow See other posts from Stack Overflow or by andy
Published on 2010-04-29T03:21:14Z Indexed on 2010/04/29 3:27 UTC
Read the original article Hit count: 215

Filed under:
|
|

hey guys

We have a "log" class which uses Relection.MethodBase to send current class info to the log.

The reflection.MethodBase stuff happens in the class itself.

However, I'd like to move that stuff to a single external "log" singleton type class.

In this scenario the external log class needs to get the CALLING info, not the current method info. I'm using stacktrace to do this, which isn't in the Reflection namespace.

Can I guarantee that "that" specific information (calling method) will be there in a production environment?

 var stackTrace = new StackTrace();
 return LogManager.GetLogger(stackTrace.GetFrame(1).GetMethod().DeclaringType);

cheers!

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET