Who calls me?
When we need to write the caller information in the log file we usually write a call like this:
_log.Error(“ThisClass.ThisMethod”, ex.Message);
The specification of the caller class and method is convenient to have information about the error location, but writing that string all the time is boring.
Searching in the .NET Class Library I found the class StackFrame that enable the program to access the application stack, so I write this piece of code:
public void Error(Exception ex) { StackFrame sf = new StackFrame(1); _log.Error( String.Concat(sf.GetMethod().DeclaringType.FullName, ".", sf.GetMethod().Name), ex.Message); }
This method navigate the stack up one level and using the StackFrame extract the caller class and the caller method and write them to the log file.
Maybe many of you already knows the StackFrame class, but the .NET Fx is big and while searching in it you always find something interesting.
Posted in Emanuele DelBono | No commentsNo comments yet. Be the first.
Leave a reply
RSS


