TextWriterTraceListener 和带有 GUID 的跟踪文件名

发布于 2024-12-05 14:41:58 字数 218 浏览 0 评论 0原文

我在应用程序中使用 TextWriterTraceListener (System.Diagnostics) 来跟踪异常等一些事情,...

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

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-12-12 14:41:58

我刚刚查看了 TextWriterTraceListener 页面下方 1/3 处有一条注释

如果尝试写入正在使用或不可用的文件,文件名会自动添加 GUID 前缀

因此,这似乎是设计使然。如果该文件确实不可用,那么当前的实现对此无能为力。您可以尝试编写 TextWriterTraceListener 的自定义实现,该实现重写相关的 Write/WriteLine 方法,以便将输出发送到每个用户的文件,并使用更适合您需求的名称。

如果您想要终端服务器上所有用户的所有日志记录都转到单个文件,那么您几乎肯定需要运行某种“第三方”进程来“拥有”该文件并同步写入它,例如由您的自定义 TextWriterTraceListener 调用的 Windows 服务

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

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

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.

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

流星番茄 2024-12-12 14:41:58

修复是否意外多次调用 Trace.Listeners.Add(xxx Listener)?

因为如果您添加了多个侦听器,那么当您调用 Trace.writeline(); 时,它们也会写入所有侦听器;

此外,当您关闭应用程序时,本地 IIS 可能会继续使用该文件。

Was the fix calling the Trace.Listeners.Add(xxx listener) multiple times on accident?

Because if you have multiple listeners added they write too all listeners when you call the Trace.writeline();

Also local IIS might be continueing to have the file in use when you shut down the application.

热血少△年 2024-12-12 14:41:58

我目前正在测试在输出方法中添加 System.Diagnostics.Trace.Listeners.Clear()...

            // Upon a new day re-create the TextWriterTraceListener to update our file name...
            if (_date?.Day != DateTime.Now.Day) { _listener = null; }

            if (_listener == null)
            {
                System.Diagnostics.Trace.Listeners.Clear();

                _fileName = $"{DateTime.Now.ToString("yyyy-MM-dd")}_Trace.json";

                // Add a writer that appends to the trace.log file:
                _listener = new System.Diagnostics.TextWriterTraceListener(_fileName);

                _listener.IndentSize = 4;
                _listener.TraceOutputOptions = System.Diagnostics.TraceOptions.None; // TraceOptions.DateTime | TraceOptions.ThreadId;
                System.Diagnostics.Trace.AutoFlush = true;
                System.Diagnostics.Trace.Listeners.Add(_listener);

                // Obtain the Console's output stream, then add that as a listener...
                System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));
            }

I am currently testing the addition of System.Diagnostics.Trace.Listeners.Clear() in my output method...

            // Upon a new day re-create the TextWriterTraceListener to update our file name...
            if (_date?.Day != DateTime.Now.Day) { _listener = null; }

            if (_listener == null)
            {
                System.Diagnostics.Trace.Listeners.Clear();

                _fileName = 
quot;{DateTime.Now.ToString("yyyy-MM-dd")}_Trace.json";

                // Add a writer that appends to the trace.log file:
                _listener = new System.Diagnostics.TextWriterTraceListener(_fileName);

                _listener.IndentSize = 4;
                _listener.TraceOutputOptions = System.Diagnostics.TraceOptions.None; // TraceOptions.DateTime | TraceOptions.ThreadId;
                System.Diagnostics.Trace.AutoFlush = true;
                System.Diagnostics.Trace.Listeners.Add(_listener);

                // Obtain the Console's output stream, then add that as a listener...
                System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文