Add logs to the start and end of methods

 MethodLogger is a macro I wrote that adds log statements at the beginning and ending of all methods in the current document in Visual Studio. Unzip the file, load it in Visual Studio using Tools->Macros->Load Macro Project, open the appropriate C# file and then run the LogMethodStartAndEnd macro. It transforms

class SomeClass
{
public void SomeMethod()
{
MessageBox.Show("Doodly doodly Doo");
}
}
to
class SomeClass
{
public void SomeMethod()
{
Console.WriteLine("Start of SomeClass.SomeMethod()");
try
{
MessageBox.Show("Doodly doodly Doo");
}
finally
{
Console.WriteLine("End of SomeClass.SomeMethod()");
}
}
}
The code, written in VB .NET (which seems to be the only supported language for writing macros?) uses Visual Studio's automation interface to get the code model for the currently open file. It then walks through all the methods, figures out the starting and ending points of each method and inserts the appropriate statements, with the most significant part being the method signature. It does a very basic check (finds the first non-empty, non comment line and checks if it's the same as the text to be logged) to make sure it doesn't modify methods it has already added logs to - when the user runs the macro twice, for example.

It should be fairly simple to change Console.WriteLine to something else - right clicking on LogMethodStartAndEnd and selecting Edit should take you to the Macro Editor IDE and lines 92, 93 contain the text to be inserted at the start and end of each method.

Published Sat, May 12 2007 1:23 by Senthil
Filed under:

Comments

# re: Add logs to the start and end of methods

Wednesday, December 19, 2007 4:30 AM by Bob

Seems like a good idea but doesn't work for me.. yet

# re: Add logs to the start and end of methods

Wednesday, December 19, 2007 4:30 AM by Bob

Seems like a good idea but doesn't work for me.. yet

Leave a Comment

(required) 
(required) 
(optional)
(required)