.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!

.TT files

I want to make a blog series about what you can do with .tt files. I do not mean now what cen be generated from edmx( I have blogged about that at

http://msprogrammer.serviciipeweb.ro/2010/09/27/generating-history-trigger-with-ef-edmx-and-tt-files/

and

http://msprogrammer.serviciipeweb.ro/2010/06/28/ef-automatic-history-of-table-and-t4-files-tt-files/

)

I will start from a .tt template that will be analyzing a class( the ViewModel in MVC terms ) and then, starting from this template, I will generate:

  1. Loading the class from config file( web / app)
  2. Generating resx files for translating
  3. Generating help / tooltip
  4. Generating logging for all methods ( with an _On convention)
  5. Generating shallow copy for the class, saving the properties and an Interface ( usually  good for DI / Ioc)
  6. Generating

I expect your suggestions too. What do you have iteratively do in your development( MVC Winking smile ) more than one time for each ViewModel ( Model) class ?

( this is the .tt file)

Friday links 34

  1. AppHarbor API Contest – AppHarbor
  2. How to use a T4 template for View Generation – ADO.NET Blog – Site Home – MSDN Blogs
  3. EF Caching with Jarek Kowalski’s Provider – ADO.NET Blog – Site Home – MSDN Blogs
  4. EF Power Tools Beta 2 Available – ADO.NET Blog – Site Home – MSDN Blogs
  5. Getting started with convention-based part registration in MEF 2 [Nick] – BCL Team Blog – Site Home – MSDN Blogs
  6. Using MEF 2 with ASP.NET MVC 3 [Nick] – BCL Team Blog – Site Home – MSDN Blogs
  7. Introducing MEF Lightweight Composition and an Updated Composition Provider for ASP.NET MVC [Nick] – BCL Team Blog – Site Home – MSDN Blogs
  8. How many Microsoft employees does it take to change a lightbulb? – Fabulous Adventures In Coding – Site Home – MSDN Blogs
  9. Past performance is no guarantee of future results – Fabulous Adventures In Coding – Site Home – MSDN Blogs
  10. Getting Started as a SharePoint Developer – Kirk Evans Blog – Site Home – MSDN Blogs
  11. Announcing the LightSwitch HTML Client! – Visual Studio LightSwitch Team Blog – Site Home – MSDN Blogs
  12. Get Online Access to 600 Microsoft Press books for $22.99 – Microsoft Press – Site Home – MSDN Blogs
  13. Announcing New Windows Azure Services to Deliver “Hybrid Cloud” – Windows Azure – Site Home – MSDN Blogs
  14. ArticleS.UncleBob.PrinciplesOfOod
  15. Programmer Time Translation Table | Passion for Coding
  16. Continuous Tests
  17. Elegant Code » CQRS à la Greg Young
  18. Doom3 Source Code Review: Introduction
  19. So You Really Want to be an Agile Developer? « Think Different
  20. Community Server Control Panel
  21. Why bother writing an Windows 8 app?
  22. Real World MVC 3 Development « Gregor’s work related blog
  23. My Journey To and From Wikis: Why I Adopted Wikis, Why I Veered Away, and a New Model | I’d Rather Be Writing
  24. How I stopped writing awesome code – jhovgaard.net
  25. Lokad.CQRS Sample Project, from Lokad
  26. Eventual consistency, CQRS and interaction design | Jimmy Bogard’s Blog
  27. We’re all out of duct tape, try putting some cheap code on it – lucisferre
  28. CQRS
  29. Rethinking web service development – Matt Aimonetti
  30. MiniProfiler
  31. Performance Considerations for EF5
  32. CLR – An Attribute-Free Approach to Configuring MEF
  33. Kinect – Starting to Develop with Kinect
  34. Linux în Windows Azure
  35. A co-Relational Model of Data for Large Shared Data Banks – ACM Queue
  36. Everything you need to know about Windows Azure Table Storage to use a scalable non-relational structured data store | Robbin Cremers vs Microsoft Technology
  37. Stevey’s Blog Rants: Rich Programmer Food
  38. Stevey’s Blog Rants: Portrait of a N00b
  39. Stevey’s Blog Rants: Programming’s Dirtiest Little Secret
  40. Basic Authentication with Asp.Net WebAPI | Steves Coding Blog
  41. Fun with action filters | Steves Coding Blog
  42. HOW TO: Use SQLite in C# Metro style app
  43. Why Your Links Should Never Say “Click Here” – UX Movement
  44. Windows Phone App Porting: Bridging the PhoneGap — Visual Studio Magazine
  45. Seeing the SQL Generated by LINQ to Entity Queries — Visual Studio Magazine
  46. The Incredibly Useful Sysinternals Suite — Visual Studio Magazine
  47. Dates and JSON – Tales from the Evil Empire
  48. If doctors would work like software engineers, no-one would consult them – Frans Bouma’s blog
  49. June Azure Events: Meet Windows Azure (June 7), Learn Windows Azure (June 11), and worldwide DevCamps – Jon Galloway
  50. Threading in C# – Free E-book
  51. A List Apart: Articles: Building Books with CSS3
  52. Using Asynchronous Methods in ASP.NET MVC 4 : Official Microsoft Site
  53. Exception Handling in ASP.NET Web API : Official Microsoft Site
  54. Amazon Web Services Aims to Make Life Easier for Windows Developers CIO.com
  55. I, Interface – CodeProject
  56. From the trenches – Improving scalability in .NET for Paycento – CodeProject
  57. Stop using AutoMapper in your Data Access Code | Blog | DevTrends
  58. Out of Duct Tape, Try Some Cheap Code – .NET Code Geeks
  59. Good Developer, Bad Developer | Guy Nirpaz Blog
  60. 12 Habits Standing Between You and What You Want
  61. Sitting is Killing You » Medical Billing and Coding Certification
  62. Validation In Razor Web Pages 2
  63. Mindscape Blog » Blog Archive » Using the ASP.NET MVC Mini-Profiler with LightSpeed
  64. Hosting Web API in Windows service at piotrwalat.net
  65. Windows 8 Metro: Asynchrony made easy
  66. Tracing Query Performance with Extended Events – Publishing | SolidQ
  67. Control the execution order of your filters in ASP.NET Web API | StrathWeb
  68. 10 resume mistakes to avoid | TechRepublic
  69. Five must-have apps for your USB stick | TechRepublic
  70. Technology – Alexis Madrigal – Sorry, Young Man, You’re Not the Most Important Demographic in Tech – The Atlantic
  71. Today Software Magazine
  72. Clarified CQRS
  73. Signs that you’re a good programmer – Software Engineering Tips
  74. Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC | XHalent Coding…

code complete 2

If you have not read this book, please stop what are you doing now and read it. After that, at the final, you will find another good reccomandations.

Now the summer holiday is coming – buy it , read it and discuss it.

powershell and clean iis

Sometimes it is necessary to remove IIS temp files from windows directory

So I have made this following script in powershell

iis reset

What it does:
restarts iis ( run as admin in Windows 7)

Cleans temporary folders from

$windir + “\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files”

$windir + “\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”

Enjoy !(iis reset)

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.