.TT files and logging

This post is a continuation of http://msprogrammer.serviciipeweb.ro/2012/07/09/tt-files/

In this post I will present a simple method to have logging methods that are called – and,if you want,you can log the values of parameters too!

Let’s say you have this code in the GUI project

ViewModelEmployeeCreate.SaveNew(emp);

and you want to log the SaveNew method – maybe to see in how much time it is executed,or to see how many times is called or simply log the method.

I will use Convention over Configuration. I will log every method that begins with _On . So I will transform the

public static void SaveNew (Employee emp)

into

public static void _OnSaveNew(Employee emp)

Then the classLogging.tt file will intercept the _On method and add this

public static void SaveNew (Employee emp)
								{
									System.Console.WriteLine(" before void _OnSaveNew (Employee emp)") ;
									try
									{
										TT_Repository.ViewModelEmployeeCreate._OnSaveNew(emp);
									}
									catch(System.Exception ex)
									{
										System.Console.WriteLine(" exception in void _OnSaveNew (Employee emp) " + ex.Message); 
										throw;
									}
									finally
									{
										System.Console.WriteLine(" after void _OnSaveNew (Employee emp)  " ); 
										
									}
								}

( you can add anything you like it – since the code is in .tt file )

So the output will be:

image

The code can be downloaded from here
and the interesting part is classLogging.tt from TT_Repository project.

Enjoy the .tt!


Posted

in

by

Tags:

Comments

4 responses to “.TT files and logging”

  1. Tudor Avatar
    Tudor

    Interesting, but I wouldn’t name a public method in C#
    _On….
    For an external user such a method looks like some internal method called when an event is triggered.

    Better use attributes to mark such methods or (even better) use some AOP-like techniques (Postsharp) to inject the logging code..

    1. Andrei Ignat Avatar
      Andrei Ignat

      The code is not full proof. It is just a work in progress. It is just to get an idea that you can do such thing.
      More, I like Postsharp. However, it is not free 😉

  2. Tudor Avatar
    Tudor

    Postsharp started edition is free (royalty free), also for commercial projects, just has a limited set of features. (http://www.sharpcrafters.com/purchase/terms?expand=starter-edition)

  3. Siderite Avatar

    I used to work with a TT file that created a proxy class from any other class. I was using reflection back then, and I had a lot of problems compiling the project and then running the .tt files and then recompiling. The DTE classes you are using seem to bypass this, using directly the source. I like that, with the caveat that it only works if you have Visual Studio installed or the DLLs published in your project. For some link spamming and the solution to small problems like syntax highlighting, check this out: http://siderite.blogspot.com/2010/06/working-with-t4.html

Leave a Reply

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