How to configure concrete LogWriter type for Logger facade?

Topics: Logging Application Block
Nov 23, 2010 at 9:39 PM

Is there a way to configure the concrete LogWriter type used by the Logger facade using the .config only?  If not, what would be the method of doing so through code, if possible?

I've tried unity using both type="LogWriter" and the namespace qualified type as shown below but without success.

  <unity>
    <typeAliases>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />      
    </typeAliases>
    <containers>
      <container name="container">
        <types>
          <type type="Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter" mapTo="Hsb.EnterpriseLibrary.Logging.AsynchronousLogWriterImpl, Hsb.EnterpriseLibrary.Logging">
            <lifetime type="singleton"/>
          </type>
        </types>
      </container>
    </containers>
  </unity>

Thank you,

Rich

Nov 24, 2010 at 12:11 AM

I assume you're using EntLib 4.1 based on the config you posted.   In the 4.1 version, the Logger class explicitly uses the LogWriter class which is a concrete class thus, you cannot configure it to use a different one.  In version 5.0 though, this is possible since it is now an abstract class.  You would use the same registration:

<unity>
    <container>
        <register type="Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
mapTo="Hsb.EnterpriseLibrary.Logging.AsynchronousLogWriter, Hsb.EnterpriseLibrary.Logging" />
    </container>
</unity>

You would then need to load the container from the config and replace the EnterpriseLibraryContainer.Current object:

IUnityContainer container = new UnityContainer();
container.LoadConfiguration();

EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@avanade.com