vendredi 30 octobre 2015

Load only one appender instead of all

I'm currently working on a console C# app and I have to log two type of data :

  • All steps of the app in general (AppLog)
  • All information that I get from a service (MachineLog)

MachineLog needs 2 propeties, but when I start the app I do not define them I have some work to do before. But I need to log from start whith AppLog so at le beginning I use :

log4net.Config.XmlConfigurator.Configure();

And it create me the C:\DNC\Suivi\Logs.txt but also C:\DNC\Suivi\(null)\(null)-logs.txt

Is there any way to only load/configure AppLog and later MachineLog ?

I tried to add parameters to Configure() and all failed.

My App.config is :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="AppLog" />
    </root>
    <logger name="Machine" additivity="false">
      <level value="ALL" />
      <appender-ref ref="MachineLog" />
    </logger>
    <appender name="AppLog" type="log4net.Appender.RollingFileAppender">
      <file value="C:\DNC\Suivi\Logs.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <appender name="MachineLog" type="log4net.Appender.RollingFileAppender" >
      <file type="log4net.Util.PatternString" value="C:\DNC\Suivi\%property{Machine}\%property{ID}-logs.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

Aucun commentaire:

Enregistrer un commentaire