Enterprise Library 6 LogCallHandler throwing exception "The LogWriter has not been set for the Logger static class"

Richard Bezerra picture Richard Bezerra · Mar 21, 2014 · Viewed 11.5k times · Source

Guys

I'm trying using LogCallHandler into Interception like this:

<interception>
        <policy name="policyLogCallHandler">
          <matchingRule name="LogsMachingRule" type="NamespaceMatchingRule">
            <constructor>
              <param name="namespaceName" value="NetTcpContracts" />
            </constructor>
          </matchingRule>
          <callHandler type="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="callHandlerLog">
            <constructor>
              <param name="eventId" value="9002"/>
              <param name="logBeforeCall" value="true"/>
              <param name="logAfterCall" value="true"/>
              <param name="beforeMessage" value="--- begin"/>
              <param name="afterMessage" value="--- end"/>
              <param name="includeParameters" value="true"/>
              <param name="includeCallStack" value="true"/>
              <param name="includeCallTime" value="true"/>
              <param name="priority" value="1"/>
              <param name="order" value="1"/>
            </constructor>            
          </callHandler>
        </policy>        
      </interception>

This configuration throw a exception: "The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method."

I found solutions for this problem using runtime configuration:

IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
LogEntry entry = new LogEntry();
entry.Message = "I am logging";
Logger.Write(entry)

But, I'm using configuration through config file. How to reproduce this behavior in config file?

Tks!

Answer

Nilesh Sawant picture Nilesh Sawant · Apr 9, 2015

The boostrapping behavior of Enterprise Library has changed in Version 6. The impact for the static Logger facade is that you need to set the internal LogWriter (for example at application start): For more details refer https://entlib.codeplex.com/discussions/442089

Thanks