How do I get a helper method in a utility class to use its callers logger in log4net?

StackOverflow https://stackoverflow.com/questions/228578

  •  04-07-2019
  •  | 
  •  

Question

I have an executable that depending on the command line switch supplied looks something like:

Program.cs -

namespace DiskSpaceReporting
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length == 1)
            {
                switch(args[0])
                {
                    case "-summarytotals":
                        SummaryDiskSpaceReporter.Run();
                        break;

                    case "-detailed":
                        DetailedDiskSpaceReporter.Run();
                        break;
                    //...other reporting types
                }



            }
        }
    }
}

SummaryDiskSpaceReporter.cs

namespace DiskSpaceReporting
{
    public class SummaryDiskSpaceReporter
    {
        private static IEventIDLog log = EventIDLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void Run()
        {
            log.Info(1234, "Starting");
            //...do work
            string message = Helpers.CreateMessage(messageID);
            //...do work
        }
    }
}

DetailedDiskSpaceReporter.cs

namespace DiskSpaceReporting
{
    public class DetailedDiskSpaceReporter
    {
        private static IEventIDLog log = EventIDLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void Run()
        {
            log.Info(1234, "Starting");
            //...do work
            string message = Helpers.CreateMessage(messageID);
            //...do work
        }
    }
}

Helpers.cs

namespace DiskSpaceReporting
{
    public class Helpers
    {
        private static IEventIDLog log = ???            
        public static string CreateMessage(Guid messageID)
        {
            log.Info(9876, "Starting");
            //...do work
        }
    }
}

In my log4net config I have two separate loggers, one for each of the SummaryDiskSpaceReporter and DetailedDiskSpaceReporter because their logging requirements are different:

<root>
    <level value="ALL" />
    <appender-ref ref="ConsoleLogAppender" />
    <appender-ref ref="EventLogAppender" />
</root>

<logger name="DiskSpaceReporting.SummaryDiskSpaceReporter">
    <appender-ref ref="SummaryDiskSpaceReporterRollingFileAppender"/>
</logger>

<logger name="DiskSpaceReporting.DetailedDiskSpaceReporter">
    <appender-ref ref="DetailedDiskSpaceReporterRollingFileAppender"/>
</logger>

Both SummaryDiskSpaceReporter and DetailedDiskSpaceReporter call a helper method in a class called Helpers. I want to put some logging into the helper class methods.

So...the question is, how do I get the Helpers.CreateMessage() method to use the same logger as its caller?

i.e.

SummaryDiskSpaceReporter.Run() -> use DiskSpaceReporting.SummaryDiskSpaceReporter logger DetailedDiskSpaceReporter.Run() -> use DiskSpaceReporting.DetailedDiskSpaceReporter logger.

Cheers Kev

Was it helpful?

Solution

I have been playing with this question for quite some time and unfortunately have to give up for a while and return to doing "real" work. For the moment what I have come up with is as follows:

Options 1: Pass the logger in

This is not a great option, but should work fine if you do not mind coupling your helper to a logger. Simply allow your CreateMessage method to take a IEventIDLog. You can then use this to log to the correct passed in logger.

public static string CreateMessage(Guid messageID, IEventIDLog log)
{
    log.Info(9876, "Starting");
    //...do work
}

Not exactly shiny, but it should work!

Options 2: Use the call stack

Make use of the call stack to find the calling code and from this find the type and get the logger you want from that type

public static string CreateMessage(Guid messageID)
{
    StackFrame frame = new StackTrace().GetFrame(1);
    IEventIDLog log = EventIDLogManager.GetLogger(frame.GetMethod().DeclaringType);
    log.Info(9876, "Starting");
    //...do work
}

Still not shiny since we need to touch the call stack and what happens if the calling code does not have a logger.

Option 3: use log4net

This is the option we want to use, hell it is what the question wants as its answer, but I have not yet worked it out yet. :) Looking through log4net we find cool things like the Hierarchy class that allows us to get the root logger.

Logger logger1 = ((log4net.Repository.Hierarchy.Hierarchy) log4net.LogManager.GetRepository()).Root;

So I am sure there is a way to get the logger one level up from CreateMessage method, now just to find it. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top