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.

3 thoughts on “Add logs to the start and end of methods”

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>