error in entlib 6 logger instantiation

Krishna Sarma picture Krishna Sarma · Nov 15, 2013 · Viewed 9.7k times · Source

I am getting an exception while setting the logwriter for Enterprise Library 6.0 logging component. On the first occurrence of logging, it succeeds. But for the consecutive calls it throws an exception: The LogWriter is already set. I have tried with checking if the Logger.Writer is not null. But this fails in the first instance, with an exception that I should set the writer using Logger.SetLogWriter.

Here is my code:

if (Logger.Writer == null)  // fails on first call
{
    IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
    LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
    Logger.SetLogWriter(logWriterFactory.Create()); // fails on subsequent calls.
}

Answer

Millaneli picture Millaneli · Feb 3, 2015

Try this:

IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);