How to check if LogWriter has been set?
Asked Answered
C

2

12

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?

Cispadane answered 31/7, 2016 at 14:13 Comment(2)
Sounds like you're supposed ensure it is set exactly once instead of testing the property. Why does that not work?Trouper
this seems to handle your issueTeamwork
C
1

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

Cispadane answered 9/8, 2016 at 20:4 Comment(0)
K
6

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

Kazak answered 4/8, 2016 at 11:43 Comment(2)
This is the way to go. If you set it once here, there should be no question on whether or not it was set.Heliotrope
Thanks for the answer, but it still doesnt answer the question.Cispadane
C
1

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

Cispadane answered 9/8, 2016 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.