Common.Logging support of logging to file.

crackhaus picture crackhaus · May 6, 2013 · Viewed 10.4k times · Source

I was looking through the documentation for common logging and didn't notice any support for writing to a log file outside of leveraging other logging technologies such as Log4Net, Enterprise library, Nlog etc. I just wanted to know if anyone knows of a way to configure Common.Logging to write to a file or if I have to fall back to another logging technology wrapped into common.logging.

Answer

Peter Ritchie picture Peter Ritchie · May 6, 2013

You pick a particular logging framework to use with Common Logging and configure that for logging to file. For example, if you wanted to use log4net with Common Logging, you might have a config file that 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"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE"/>
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender" >
  <param name="File" value="log.txt" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="FileAppender" />
</root>
  </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.1.2.0" newVersion="2.1.2.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>