Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Jobs or Task Scheduler call log4net, does not write log file

Tags:

log4net

I write a console application with log4net, my log4net config below

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="Process_Log\" />
    <appendToFile value="true" />
    <datePattern value="yyyy-MM-dd.TXT" />
    <maxSizeRollBackups value="10" />
    <rollingStyle value="Date" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %message [%logger] %n" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

Run the console application, It has output log files. But use SQL Server Agent Jobs or Windows Task Scheduler call it, It didn't write log file.

like image 839
Robin Li Avatar asked Oct 24 '14 10:10

Robin Li


People also ask

Where is log file log4net?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.


1 Answers

I figured out the problem. When you run the solution from Visual Studio, log4net finds out the configuration file from the "bin" folder. So, it gets required information to run. But when you run the application by Windows Task Scheduler, the situation is different. This time, the Windows Task Scheduler tries to find out the Configuration file from this location "C:\Windows\System32". Because, when you run Windows Task Scheduler, it treats the default application folder location "C:\Windows\System32".

You can write following codes to navigate the application to load the log4net configuration file. Here, the config file name is "LogConfiguration.config".

 var log4NetConfigDirectory =
       AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
 var log4NetConfigFilePath = Path.Combine(log4NetConfigDirectory, "LogConfiguration.config");
 log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigFilePath));

like image 193
user3626918 Avatar answered Oct 01 '22 05:10

user3626918