none
Reflection or hard-coding method name when logging? RRS feed

  • Question

  •         public void GetAllTradesTest()
            {
                log.Info("GetAllTradesTest: Starting");
    
                TradeService target = new TradeService();
                List<Trade> actual = target.GetAllTrades();
    
                log.Info("GetAllTradesTest: Ending");
            }
    OR
            public void GetAllTradesTest()
            {
                log.Info(MethodBase.GetCurrentMethod().Name + " : Starting");
    
                TradeService target = new TradeService();
                List<Trade> actual = target.GetAllTrades();
    
                log.Info(MethodBase.GetCurrentMethod().Name + ": Ending");
            }

    Which do you prefer?  I hate hard-coding the name, not only do you have to maintain it if you change the method name but also it's difficult to copy/paste into other methods.  The reflection based solution is elegant, however as far as I know Reflection is slow and should be avoided.  Also now I wonder if MethodBase.GetCurrentMethod().Name would be available in Release build, I don't know if it needs debugging symbols, I guess I can give it a test.
    Tuesday, September 15, 2009 6:00 PM

Answers

  • a bit of googling and...


    the home page of the Enterprise Library 4.1 - October (currently most recent) is http://msdn.microsoft.com/en-us/library/dd203099.aspx . From there you can download the hands-on labs which has some easy to understand examples on how to use the Logging & Policy Injector (btw, you can also opt on using Unity as your IoC container, there are samples for that as well).

    The good news is that there's a very active community supporting these tools, so you might also want to take a look into this codeplex site http://www.codeplex.com/entlib that has quite good content, including discussion groups and videos for self-training (though i haven't seen the vids, so i can't comment how good they are).


    good luck! :)

    Fernando Felman
    Solution Architect
    Unique World
    My blog

    If your question was answered, please don't forget to mark it so.

    • Edited by Fernando Felman Thursday, September 17, 2009 12:51 AM url typo
    • Marked as answer by e36M3 Monday, September 21, 2009 4:55 PM
    Thursday, September 17, 2009 12:49 AM

All replies

  • Another idea ...

    Have the Log.Info method determine the method that called it and display the value. That way you don't need to specify it at all when it is called.

    I have some information about that on my blog here:

    http://msmvps.com/blogs/deborahk/archive/2009/07/28/caller-id-using-stacktrace.aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, September 15, 2009 7:28 PM
  • hmm, depends.
    if it's for unit test (which i think that's what method name implies), then performance is not an issue. in which case, i'd stick with the 2nd approach which is more flexible and easier to manage.

    if, on the other hand, the method is a "real" logic method (not a unit test), then i wouldn't use any of those approach. what bugs me about the solutions you presented is that it breaches the separation of concern principle because the method not only deals with the logic concern (by using the TradeService) but also with the logging concern. this has a potential of causing all heaps of issues in the future.

    a different approach that follows the separation of concerns principle would be using IoC (inversion of control ) and DI (dependency injection ). For example, Microsoft provides the Enterprise Library offering the Logging Application Block that when initiated from the Policy Injector , it allows you to format logging message templates, including the method name. I think using an IoC container and injecting a logging component would make your code much easier to maintain, and it will allow you to add the method names and any other information you might need by authoring a generic template.

    HTH

    Fernando Felman
    Solution Architect
    Unique World
    My blog

    If your question was answered, please don't forget to mark it so.

    Tuesday, September 15, 2009 11:38 PM
  • Fernando, do you have an example of logging configured via IoC/DI?

    Thanks.
    Wednesday, September 16, 2009 1:20 PM
  • a bit of googling and...


    the home page of the Enterprise Library 4.1 - October (currently most recent) is http://msdn.microsoft.com/en-us/library/dd203099.aspx . From there you can download the hands-on labs which has some easy to understand examples on how to use the Logging & Policy Injector (btw, you can also opt on using Unity as your IoC container, there are samples for that as well).

    The good news is that there's a very active community supporting these tools, so you might also want to take a look into this codeplex site http://www.codeplex.com/entlib that has quite good content, including discussion groups and videos for self-training (though i haven't seen the vids, so i can't comment how good they are).


    good luck! :)

    Fernando Felman
    Solution Architect
    Unique World
    My blog

    If your question was answered, please don't forget to mark it so.

    • Edited by Fernando Felman Thursday, September 17, 2009 12:51 AM url typo
    • Marked as answer by e36M3 Monday, September 21, 2009 4:55 PM
    Thursday, September 17, 2009 12:49 AM
  • Thanks Ferdnando, I will take a look.  I wonder if it'll work with log4net.
    Monday, September 21, 2009 4:55 PM