How to check if LogWriter has been set?

user919426 picture user919426 · Jul 31, 2016 · Viewed 10.5k times · Source

I am trying to handle an Enterprise Library 6 LogWriter Exception that has recently popped up after upgrading from Enterprise Library 4 to 6.

I either get:

The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method.

OR

The LogWriter is already set.

...depending on the scenario.

The problem is that it throws an InvalidOperationException which seems too generic to handle, and that even checking using

if (Logger.Writer == null)

... also yields an exception, so how would one then check if the writer is set or not?

Answer

Anton Gogolev picture Anton Gogolev · Aug 4, 2016

According to this CodePlex discussion,

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)

If you're in a web application scenario, Application_Start() is the good way to do so:

protected void Application_Start()
{
    Logger.SetLogWriter(new LogWriterFactory().Create());
}

Otherwise, set things up in Main() method (or somewhere around it -- say, during container initialization).