2012-02-28 216 views
2

我想添加日誌記錄到我用Quartz.net使用common.logging編寫log4net的日誌文件構建的Windows Web服務。log4net日誌文件沒有被寫入使用Quartz.net + Common.Logging

我的App.config看起來是這樣的:

<?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> 

我然後寫一些日誌上調用onStart()事件:

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

過程開始罰款,但沒有日誌文件?

我錯過了什麼?

回答

4

您必須將等級添加到您的factoryAdapter

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

並檢查您是否使用了正確的Common.Logging版本。 Quartz.net 1.0.3使用Common.Logging版本1.2。

您可以使用此作爲sample和其他一些信息here

UPDATE

你的項目必須引用這些程序集:

  1. Common.Logging.dll(版本1.2.0.0)
  2. Common.Logging.Log4Net.dll(版本1.2 .0.2)
  3. log4net.dll(版本1.2.10.0)

,這是您的app.config(沒有石英部分):

<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> 

我已經上傳了一個樣本項目here(QuartzTestLog4Net.zip)。
您可以將此項目添加到從SourceForge下載的solution
官方文件是here

正如Martinffx指出的那樣,如果您使用的是<arg key="configType" value="INLINE" />,則不需要在factoryAdapter節中指定級別原因,在這種情況下,log4net將簡單地使用配置文件中也存在的XML配置。

+0

謝謝,但仍然沒有。另外,如果配置是INLINE,它不會從log4net配置中獲得級別? – Martinffx 2012-02-29 11:15:46

+1

@Martinffx:你有沒有試過我的示例代碼?它必須工作。你是對的;對於configType鍵的INLINE值,Common.Logging將只是初始化log4net,它將簡單地使用配置文件中也存在的XML配置。 – LeftyX 2012-03-01 09:37:44

+0

有類似的問題...偉大的解決方案!非常感謝! – 2012-10-31 17:02:48

0

如果您沒有以管理員身份運行服務,那麼這可能是一個權限問題。

由於您沒有指定log.txt文件的創建位置,因此它可能嘗試在類似C:\ Windows \ System32的文件夾中創建該文件,並且服務可能沒有寫入權限夾。

+0

我已經檢查過。該文件在應用程序的基本目錄中創建。 – LeftyX 2012-02-29 16:36:09

+0

如果你有權訪問服務器,你可以嘗試運行Sysinternal的DebugView,它可能會指出一些錯誤。您可能需要在debugview中啓用Capture Global Win32輸出,並通過將''添加到webservice app.config的appSettings部分來啓用內部lo​​g4net調試 – sgmoore 2012-02-29 18:05:02