how do you make log4net output to current working directory?

后端 未结 2 1781
温柔的废话
温柔的废话 2021-02-20 06:58

Is it possible to have log4net put its log files relative to the current working directory instead of the directory where the application resides?

In other words, if I

2条回答
  •  青春惊慌失措
    2021-02-20 07:20

    Not possible from the config file, as per here. It may be possible if you are configuring it manually from inside your program though:

    public static log4net.Appender.IAppender CreateFileAppender(string name,
    string fileName)
    {
      log4net.Appender.FileAppender appender = new
    log4net.Appender.FileAppender();
      appender.Name = name;
      appender.File = fileName;
      appender.AppendToFile = true;
    
      log4net.Layout.PatternLayout layout = new
    log4net.Layout.PatternLayout();
      layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
      layout.ActivateOptions();
    
      appender.Layout = layout;
      appender.ActivateOptions();
    
      return appender;
    }
    

    You can then associate it with the logger as follows:

    AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender",   
    Path.Combine(Directory.GetCurrentDirectory(), "foo.log")));
    

提交回复
热议问题