views:

317

answers:

2

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

+2  A: 
FryHard
Kev
A: 

Did you ever figure this out?

I went for option 2, not pretty but did the job.
Kev