Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 58465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:51:08+00:00 2026-05-10T17:51:08+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:51:08+00:00Added an answer on May 10, 2026 at 5:51 pm

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a bug May 11, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer Give Metabase Explorer a shot as part of the IIS… May 11, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer If you want to stay with .net 2.0, use remoting.… May 11, 2026 at 9:27 pm

Related Questions

My question is pretty straightforward: You are an executable file that outputs Access granted
My objective is to write a program which will call another executable on a
If I have a makefile that calls another makefile, how to I get the
I have an executable that is started by a windows service, this program will

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.