gpt4 book ai didi

c# - 关闭/重新打开使用 XML 配置创建的 TraceListener,指向外部 USB 内存棒

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:27 27 4
gpt4 key购买 nike

我有一个 TraceListener 对象,用于记录,在 app.config 中创建,指向 USB 内存棒上的一个位置。

我收到一个带有消息的 IOException

The volume for a file has been externally altered so that the opened file is no longer valid.

在我移除设备后尝试 Trace.WriteLine() 然后将其重新插入时。我知道它正在发生,因为 Trace 监听器具有的打开文件的句柄已失效当我拔掉它。

我已经通过在 TraceListener 上调用 Close() 设法阻止了 IOException,但我不知道如何重新打开句柄以同一个文件。 TextWriterTraceListener.Close() 方法的 MSDN 文档说

Calling a Write or WriteLine method after calling Close automatically reopens the stream.

但它就是不这样做。关闭流后我没有得到进一步的输出。

编辑:

更多信息

我发现只有在我调用 Close() 后尝试在拔下驱动器时写入时,Trace Listener 才能再次写入。如果我在拔下驱动器时不写入,然后再次插入驱动器并尝试写入,一切正常。我还验证了我的 Trace.Listeners 集合仍然包含我的监听器。

最佳答案

经过大量研究,我发现 Trace.Refresh() 似乎可以解决这个问题,但是从 rene 阅读原始问题的评论来看,此方法将重新初始化整个 Trace 框架。对于我想要的东西来说似乎有点沉重,但也许这个答案会对其他人有所帮助。

Trace.Refresh() on MSDN

来自 MSDN 的注释:

Trace configuration data is captured when the application is started. If the configuration data is changed after the application is started, call the Refresh method to update the trace configuration data.

关于c# - 关闭/重新打开使用 XML 配置创建的 TraceListener,指向外部 USB 内存棒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22565688/

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