简体   繁体   English

如何检查是否已设置LogWriter?

[英]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. 我正在尝试处理从Enterprise Library 4升级到6后最近弹出的Enterprise Library 6 LogWriter异常。

I either get: 我得到:

The LogWriter has not been set for the Logger static class. 尚未为Logger静态类设置LogWriter。 Set it invoking the Logger.SetLogWriter method. 设置它调用Logger.SetLogWriter方法。

OR 要么

The LogWriter is already set. LogWriter已设置。

...depending on the scenario. ......取决于场景。

The problem is that it throws an InvalidOperationException which seems too generic to handle, and that even checking using 问题是它抛出一个InvalidOperationException ,它似乎太通用了,甚至无法检查使用

if (Logger.Writer == null)

... also yields an exception, so how would one then check if the writer is set or not? ...也产生异常, 那么如何检查作者是否被设置?

According to this CodePlex discussion , 根据CodePlex的讨论

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) Enterprise Library的boostrapping行为在版本6中已更改。静态Logger外观的影响是您需要设置内部LogWriter (例如在应用程序启动时)

If you're in a web application scenario, Application_Start() is the good way to do so: 如果您处于Web应用程序场景中, Application_Start()是执行此操作的好方法:

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

Otherwise, set things up in Main() method (or somewhere around it -- say, during container initialization). 否则,在Main()方法(或其周围的某个地方 - 例如,在容器初始化期间)进行设置。

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 LogWriterNotSetExceptionLogWriterAlreadySetException

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: 根据关于Unity未来的通知评论 ,来自P&P成员:

For the Logging Application Block, we consider it to be superceded by Semantic Logging (formerly Semantic Logging Application Block or SLAB). 对于日志记录应用程序块,我们认为它被语义记录(以前称为语义记录应用程序块或SLAB)取代。

https://github.com/mspnp/semantic-logging 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. 换句话说,我们不打算处理Logging Application Block,我们也没有计划将其转移给新的所有者。

So best bet for anyone working on something new is trying out Semantic Logging 因此,对于从事新事物的人来说,最好的选择是尝试语义记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM