Question

I'm trying to add logging to my windows web service built with Quartz.net using common.logging to write a log file with log4net.

My App.config looks like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
    </sectionGroup>     
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="CommerceScheduler" />
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="Normal" />
  </quartz>
  <appSettings>
    <add key="configpath" value="C:\Projects\SiteScheduler\SiteScheduler\Schedule.xml"/>
  </appSettings>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
</log4net>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
   </runtime>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

I then write some logs on the onStart() event:

protected override void OnStart(string[] args)
{    
    var log = LogManager.GetCurrentClassLogger();

    // construct a scheduler factory
    ISchedulerFactory schedFact = new StdSchedulerFactory();

    // get a scheduler
    var sched = schedFact.GetScheduler();
    sched.Start();

    log.Debug(m => m("Scheduler started"));  
    log.Debug(m => m("Load Schedules"));
    ProcessLogs("Scheduler started");
    LoadSchedules(sched);       
}

The process starts fine, but no log files?

What am I missing?

Was it helpful?

Solution

You have to add the levels to your factoryAdapter.

<common>
  <logging>
    <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</common>

and check you're using the using the right Common.Logging version. Quartz.net 1.0.3 uses the Common.Logging version 1.2.

You use this as a sample and some other infos here.

UPDATE:

You project must reference these assemblies:

  1. Common.Logging.dll (ver. 1.2.0.0)
  2. Common.Logging.Log4Net.dll (ver 1.2.0.2)
  3. log4net.dll (ver 1.2.10.0)

and this is your app.config (without the quartz section):

<configSections>
  <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <sectionGroup name="common">
    <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
  </sectionGroup>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE"/>
      </factoryAdapter>
    </logging>
</common>

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="MyQuartzLog.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

I've uploaded a sample project here (QuartzTestLog4Net.zip).
You can add this project to the solution downloaded from SourceForge.
Official documentations is here.

As pointed out by Martinffx if you're using <arg key="configType" value="INLINE" /> you don't need to specify the levels in the factoryAdapter section cause, in this situation, log4net will simply use the XML configuration that is also present in your config file.

OTHER TIPS

If you are not running the service as an administrator, then this could this be a permissions issue.

Since you are not specifying where the log.txt file will be created, it is possible that it is attempting to create the file in a folder like C:\Windows\System32 and the service may not have write access to that folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top