Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if LogWriter has been set?

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?

like image 693
user919426 Avatar asked Jul 31 '16 14:07

user919426


2 Answers

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).

like image 189
Anton Gogolev Avatar answered Sep 21 '22 05:09

Anton Gogolev


Thanks for the answers and comments.

I have looked through the project's code and seen that there is nothing built into it that supports this.

Even though the project is no longer under development, I took a chance and posted a feature request.

Best scenario to achieve this requirement would be forking downloading it and adding logic that does a check and additionally defining specific exceptions (see feature request):

LogWriterNotSetException and LogWriterAlreadySetException

EDIT

Struck out forking as that would have licensing implications. Ownership has not been transfered for the Logging Application Block. Only Unity and Prism have been transferred.

According to a comment on the notice about the future of Unity, from a P & P member:

For the Logging Application Block, we consider it to be superceded by Semantic Logging (formerly Semantic Logging Application Block or SLAB).

https://github.com/mspnp/semantic-logging

In other words, we do not intend to work on the Logging Application Block and we have no plan to transfer it to new owners.

So best bet for anyone working on something new is trying out Semantic Logging

like image 25
user919426 Avatar answered Sep 17 '22 05:09

user919426