TextWriterTraceListener会和跟踪文件名用的GUID文件名、TextWriterTraceListener、GUID

2023-09-03 04:48:09 作者:服软

我使用 TextWriterTraceListener会( System.Diagnostics程序)在我的应用程序,跟踪像例外几件事情,...

I use TextWriterTraceListener (System.Diagnostics) in my application to trace several things like exceptions,...

应用程序在终端服务器上运行,如果有同时使用了很多用户的监听器开始创建许多跟踪文件随机的GUID 中的文件名。

The application is running on a terminal server and if there are many users using it simultaneously the listener starts to create many tracefiles with random GUIDs in the filename.

是否有可能或解决方法来避免这种行为?

Are there possibilities or workarounds to avoid this behaviour ?

推荐答案

我刚一看的TextWriterTraceListener并有一记关于在页面的方式1/3

I've just taken a look at the documentation for TextWriterTraceListener and there's a note about 1/3 of the way down the page

如果试图写入一个文件,该文件正在使用中或不可用时,文件名会自动$由GUID pfixed p $

If an attempt is made to write to a file that is in use or unavailable, the file name is automatically prefixed by a GUID

那么,这似乎是设计使然。如果该文件确实无法再没有什么能与当前的实现来完成这件事。什么,你可以尝试做的是写一个自定义实现TextWriterTraceListener会的,它覆盖了相关的写/的WriteLine方法,从而使输出到一个文件中,每个用户,一个名称更适合您的需求。

So, this would appear to be by design. If the file is indeed unavailable then there's nothing that can be done about it with the current implementation. What you could try doing is writing a custom implementation of TextWriterTraceListener that overrides the relevant Write/WriteLine methods so that the output goes to a file, per user, with a name that better suits your needs.

如果你想要的是所有的终端服务器上的所有用户登录去到一个文件,那么你几乎肯定需要有某种形式的第三方的进程中运行拥有的文件,同步更新写入它,如Windows服务,然后由您自定义的TextWriterTraceListener会

If what you want is for ALL logging from ALL users on the Terminal Server to go to a single file, then you'll almost certainly need to have some kind of "3rd party" process running that "owns" the file and synchronises writes to it, such as a Windows Service that is then called by your custom TextWriterTraceListener