gpt4 book ai didi

c# - 如何检查是否设置了 LogWriter?

转载 作者:可可西里 更新时间:2023-11-01 09:13:32 28 4
gpt4 key购买 nike

我正在尝试处理从 Enterprise Library 4 升级到 6 后最近弹出的 Enterprise Library 6 LogWriter Exception。

我要么得到:

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

The LogWriter is already set.

...取决于场景。

问题是它会抛出一个InvalidOperationException,这看起来太普通了,无法处理,甚至检查使用

if (Logger.Writer == null)

... 也会产生异常,那么如何检查编写器是否已设置?

最佳答案

根据 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)

如果您处于 Web 应用程序场景中,Application_Start() 是这样做的好方法:

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

否则,在 Main() 方法(或它周围的某个地方——比如,在容器初始化期间)中进行设置。

关于c# - 如何检查是否设置了 LogWriter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684868/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com