自定义 TraceListener Windows 应用程序

发布于 2024-10-14 22:08:54 字数 332 浏览 1 评论 0原文

到目前为止,在我的应用程序中,我将所有跟踪事件记录到硬盘驱动器上的文件中。

我现在想以单独的 Windows 跟踪应用程序的形式增强它,该应用程序侦听

来自主应用程序的跟踪(当它们生成时)并在

类似 gridview 的界面上报告它。现在的问题是:

我必须使用哪种 TraceListener 才能在读取日志信息的速度方面获得最大收益?

限制 由于某些限制,我无法使用数据库日志记录和读取方法,

监听应用程序 eventLogs 有什么帮助吗?

感谢您的建议和时间。

So far in my application i am logging all the trace events to a file on my hard drive.

I now want to enhance it in the form of a separate windows trace application which listens

to the trace from the Main application (as they are generated) and report it on a

gridview like interface. Now the question is :

What kind of TraceListener i have to use to get a maximum benefit interms of speed at which the log information is read ?

Restrictions
Owing to certain restrictions, i cannot use the database logging and reading approach

Will listening to the application eventLogs help in any way?

Thanks for the suggestions and time.

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

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

发布评论

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

评论(2

扮仙女 2024-10-21 22:08:54

默认跟踪侦听器将使用 win32 api OutputDebugString。您可以使用现有工具侦听传递给此方法的消息。看看这个,例如:

http://technet.microsoft.com/ en-us/sysinternals/bb896647.aspx

也许这会节省您编写自己的时间。

如果您确实想编写自己的代码,并且主要关心的是尽快将跟踪消息发送到跟踪查看器应用程序,那么您可以让 TraceListener 接受来自查看器的网络连接。每当跟踪侦听器处理跟踪消息时,您都会写入网络。如果您不关心能否在远程计算机上查看跟踪消息,那么当然,侦听放入 OutputDebugString 的内容也是一种选择。

当然,这会影响执行跟踪的应用程序的性能,因此写入网络最好异步完成,而不会阻止跟踪写入调用。在写入网络时,您必须将跟踪消息添加到队列中进行处理。

这是一个可能有效的简单示例:

    public class NetworkPublishingTraceListener : TraceListener {

    private List<string> messages = new List<string>();
    private AutoResetEvent messagesAvailable = new AutoResetEvent(false);
    private List<TcpClient> traceViewerApps;
    private object messageQueueLock = new object();

    public NetworkPublishingTraceListener(int port) {
        // Setup code for accepting and dealing with network connections. 
        (new Thread(BackgroundThread) { IsBackground = true }).Start();
    }

    public override void Write(string message) {
        if (traceViewerApps.Count == 0) {
            return;
        }

        lock (messageQueueLock) {
            messages.Add(message);               
        }
        messagesAvailable.Set();
    }

    public override void WriteLine(string message) {
        Write(message + Environment.NewLine);
    }

    private void BackgroundThread() {
        while (true) {
            messagesAvailable.WaitOne();
            List<string> messagesToWrite;
            lock (messageQueueLock) {
                messagesToWrite = messages;
                messages = new List<string>();
            }
            traceViewerApps.ForEach(viewerApp => {
                StreamWriter writer = new StreamWriter(viewerApp.GetStream());
                messagesToWrite.ForEach(message => writer.Write(message));
            });
        }
    }
}

The default trace listener will use the win32 api OutputDebugString. You can listen to messages passed to this method using existing tools. Have a look at this, for instance:

http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

Maybe that will save you the time it takes to write your own.

If you do want to write your own and your main concern is to get trace messages as fast as possible to the trace viewer application, then you could have your TraceListener accept network connections from the viewer. Whenever a trace messages is handled by the trace listener, you would write to the network. If you're not concerned about being able to view trace messages on a remote machine, then listening to whats put to OutputDebugString is also an option, of course.

This would effect the performance of the application doing the tracing of course so writing to the network is best done asynchronously without blocking the trace write call. While writing to the network you would have to add trace messages to a queue to process.

Here is a simple example which probably works:

    public class NetworkPublishingTraceListener : TraceListener {

    private List<string> messages = new List<string>();
    private AutoResetEvent messagesAvailable = new AutoResetEvent(false);
    private List<TcpClient> traceViewerApps;
    private object messageQueueLock = new object();

    public NetworkPublishingTraceListener(int port) {
        // Setup code for accepting and dealing with network connections. 
        (new Thread(BackgroundThread) { IsBackground = true }).Start();
    }

    public override void Write(string message) {
        if (traceViewerApps.Count == 0) {
            return;
        }

        lock (messageQueueLock) {
            messages.Add(message);               
        }
        messagesAvailable.Set();
    }

    public override void WriteLine(string message) {
        Write(message + Environment.NewLine);
    }

    private void BackgroundThread() {
        while (true) {
            messagesAvailable.WaitOne();
            List<string> messagesToWrite;
            lock (messageQueueLock) {
                messagesToWrite = messages;
                messages = new List<string>();
            }
            traceViewerApps.ForEach(viewerApp => {
                StreamWriter writer = new StreamWriter(viewerApp.GetStream());
                messagesToWrite.ForEach(message => writer.Write(message));
            });
        }
    }
}
恰似旧人归 2024-10-21 22:08:54

为什么不使用 log4netnlog

Why don't you use log4net or nlog ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文