Log Method Name Helper

Posted by: Steven Smith, on 29 May 2011 | View original | Bookmarked: 0 time(s)

Sometimes its handy to see the order in which methods are firing, or how long theyre taking, without having to attach a debugger.  Typically, you might write some code like this:

public void Foo()
{
  Debug.Print("Entering Foo");
  // other stuff
}
public void Bar()
{
  Debug.Print("Entering Bar");
  // other stuff
}

This of course gets tedious after a while.  There are all kinds of things wrong with this approach.  It isnt DRY.  It includes magic strings.  Youd almost never need it if you were following TDD.  Etc.  If you really need this kind of logging detail on a lot of methods, Id encourage you to investigate AOP options like PostSharp.  However, if youre not quite ready for that but do want to at least eliminate the magic strings from the above code, you can use this little helper to get the type and name of the method for your logging purposes:

private static void LogMethod()
{
    var stackTrace = new StackTrace();
    var method = stackTrace.GetFrame(1).GetMethod();
    Debug.Print(method.ReflectedType.Name + "." + method.Name);
}

Drop that into the class youre working in, and replace your calls with a call to LogMethod().  Naturally if you need to record both entering and existing of each method, you can adjust to something like this:

private static void LogMethodStart()
{
    var stackTrace = new StackTrace();
    var method = stackTrace.GetFrame(1).GetMethod();
    Debug.Print("Entering " + method.ReflectedType.Name + "." + method.Name);
}
private static void LogMethodEnd()
{
    var stackTrace = new StackTrace();
    var method = stackTrace.GetFrame(1).GetMethod();
    Debug.Print("Exiting " + method.ReflectedType.Name + "." + method.Name);
}
 

For obvious reasons you should avoid leaving these in your production code.  One easy way to ensure this is the use of the Conditional attribute.  By adding this attribute and specifying DEBUG you can ensure these methods are not even included in your DLLs when you perform a Release build.  Heres an example showing this attribute in use:

[Conditional("DEBUG")]
private static void LogMethod()
{
    var stackTrace = new StackTrace();
    var method = stackTrace.GetFrame(1).GetMethod();
    Debug.Print(method.ReflectedType.Name + "." + method.Name);
}

Naturally if youre going to be using this in many classes, you can add it to a common helper class.  Note that if you decide to refactor out the duplication in the above Start/End methods, youll most likely need to adjust the hard-coded GetFrame(1) to use a higher number, since youll need to unwind the stack further to get to the method youre actually interested in (most likely just incrementing it to 2 will do the trick, assuming you only create one more method).


Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Refactor | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 436 | Hits: 21

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD