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.
}
Try this:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);