.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:
The code can be downloaded from here
and the interesting part is classLogging.tt from TT_Repository project.
Enjoy the .tt!
4 Responses