0

I had a console application that could be multiple instances running at a time. Each instance generates an output file, in a path specified in the command line parameters. I want the console application to create a log file (FileAppender) in the same directory as the output file. This will ensure that multiple instances don't write to the same log file, and the log for an instances is in the same location as the output file. Here is how I create and use my logger (in each class and project)

private static readonly ILog Log = 
                        LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

...

// Inside a method
Log.InfoFormat("Output file ({0}) exists, deleting.", _commandLineOptions.OutputFile);

I was hoping that I could use an approach similar to this question

Community
  • 1
  • 1
BCarlson
  • 1,122
  • 2
  • 11
  • 26
  • .. so why don't you? – stuartd May 01 '15 at 08:40
  • That's what I did. I moved the GetLogger in to the constructor, and prior to calling GetLogger I set the path. Then loggers created in other modules and assemblies retained the path. – BCarlson May 01 '15 at 12:25

1 Answers1

0

Previous Code....

class Program
{
   private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

   static void Main(string[] args)
   {
     ...
   }
   ...
}

Solution

class Program
{
   private static ILog _log;

   static void Main(string[] args)
   {
     var outputPath = Path.GetDirectoryName(_commandLineOptions.OutputFile);
     GlobalContext.Properties["logpath"] = outputPath;
     _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     ...
   }
   ...
}
BCarlson
  • 1,122
  • 2
  • 11
  • 26