4

I am using Prism 4 and want to allow my various modules to log using log4net. At the moment I have the following ILoggerFacade implementation:

public class CustomLogger : ILoggerFacade
{
    private readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().GetType());

    static CustomLogger()
    {
        XmlConfigurator.Configure();
    }

    public void Log(string message, Category category, Priority priority)
    {
        switch (category)
        {
            case Category.Debug:
                _logger.Debug(message);
                break;

            case Category.Info:
                _logger.Info(message);
                break;

            case Category.Warn:
                _logger.Warn(message);
                break;

            case Category.Exception:
                _logger.Error(message);
                break;
        }
    }
}

This works fine, my modules have an ILoggerFacade injected and can log via that, which end up here and end up in log4net. My problem is log entries lose the call site information as they are using the CustomLogger's logger instance so end up looking like this:

System.Reflection.RuntimeConstructorInfo: 2012-10-11 15:41:07,486 [23] INFO  - test log

has anyone managed to solve this problem? I have thought about passing in the Type of the caller but it seems a little clunky having to do that for every log call in my modules.

I am unable to use .Net 4.5 so can't use CallerMemberNameAttribute unfortunately.

Dutts
  • 5,781
  • 3
  • 39
  • 61
  • 2
    I feel like the easiest solution is to not use ILoggerFacade at all and just add log4net to all of my modules under one common config but that doesn't feel very "prism"-y – Dutts Oct 11 '12 at 15:10
  • 3
    You can do this by following the instructions here: http://stackoverflow.com/a/3448439/106567 – Stefan Egli Oct 12 '12 at 15:21
  • Thanks @StefanEgli, I'll take a look into this. For some reason I didn't spot this one on my original search. – Dutts Oct 19 '12 at 07:47
  • I actually had difficutlies to find it myself even though I knew it was there... in the end I had to include my name in the "search string" :-) – Stefan Egli Oct 19 '12 at 08:06

0 Answers0