1

I would like to log the start and ending of a method, but the code is messy and is hard to read:

void mymethod()
{
   LogUtility.EnteringMethod();
   //dowork
   LogUtility.EXitingMethod();
}

I would like to convert the above to just be

void mymethod()
{
   //dowork
}

But at compile/build time, I would like a macro/script to add those LogUtility lines in every one of my methods.

I know there is aspect-oriented programming, but I am looking for something significantly less complex and clunky.

Can you recommend a way to automatically generate code right before a compile/build?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • You're looking for PostSharp. – SLaks Feb 21 '16 at 02:01
  • If you are looking for more old-school approach - "logging like cross-cutting concern with dependency injection" may be good search term (not really close to what your have in mind, but achieves similar goal) I.e. http://stackoverflow.com/questions/7905110/logging-aspect-oriented-programming-and-dependency-injection-trying-to-make. – Alexei Levenkov Feb 21 '16 at 02:03
  • Take a look at frameworks, like [Fody (free)](https://github.com/Fody/Anotar) or [PostSharp (commercial)](https://www.postsharp.net/diagnostics/net-logging) which provide this functionality by applying compile time weaving. – isaias-b Feb 21 '16 at 02:04
  • @isaias-b trying to stay away from postsharp, it's way overkill. i just want a simple pre-build script or something – Alex Gordon Feb 21 '16 at 02:08
  • @SLaks im not, its way too overkill – Alex Gordon Feb 21 '16 at 02:13

2 Answers2

2

if you want an automatic way, an AOP framework like PostSharp is pretty much the cleanest option.. you could try to automate it yourself with reflection and attributes and IL manipulation, but that is not trivial.

and if you want to really keep it simple and not use 100% automation, you can make use of a wrapper method, with an Action parameter. this is really not a scalable solution and is pretty ugly in terms of production-code-readability.

public void RunMethodWithEntryAndExitLogging(Action methodToExecute)
{
   LogUtility.EnteringMethod(); // use methodToExecute.Method.Name if you need it

   methodToExecute(); // the method that is the actual work

   LogUtility.EXitingMethod();
}

as i said, this is just a quick and simple way. but gets the job done for a specific problem you might be trying to solve.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
1

I know this answer might be late, but one of the solutions for generating code at build time would be to use the T4 Editor.

This happens to be the same engine that Entity Framework and MVC use to generate the c# after interpreting your models and such.

If you would like the official documentation on how it works from microsoft you can also read here Code Generation and T4 Text Templates, Design-Time Code Generation by using T4 Text Templates.

With this you can in theory create, for instance according to attributes you add to your methods, even on build and compile, such a .tt file which can contain logging, authorization or other handy extensions before your methods would run in your classes thus eliminating lots of boiler plate code, but maintenance and debugging might be a complex task.

If you do not want to have the hassle of handling all this yourself then you can use Post Sharp as Raja Nadar pointed out in his answer.

Hope this gives you a place to start.