【问题标题】:Setting FileLogger path using LogManager使用 LogManager 设置 FileLogger 路径
【发布时间】:2015-04-30 14:42:38
【问题描述】:

我有一个控制台应用程序,它可以同时运行多个实例。每个实例在命令行参数中指定的路径中生成一个输出文件。我希望控制台应用程序在与输出文件相同的目录中创建一个日志文件 (FileAppender)。这将确保多个实例不会写入同一个日志文件,并且一个实例的日志与输出文件位于同一位置。以下是我创建和使用记录器的方式(在每个类和项目中)

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

...

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

我希望我可以使用类似于this question的方法

【问题讨论】:

  • .. 那你为什么不呢?
  • 我就是这么做的。我将 GetLogger 移到构造函数中,并在调用 GetLogger 之前设置了路径。然后在其他模块和程序集中创建的记录器保留路径。

标签: log4net log4net-configuration log4net-appender


【解决方案1】:

以前的代码....

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

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

解决方案

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);
     ...
   }
   ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2016-06-30
    • 2012-01-21
    • 2017-08-02
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多