捕获服务进程的跟踪消息 [英] Capture the Trace Messages of the Processes of an service

查看:71
本文介绍了捕获服务进程的跟踪消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的英语不是应该的那么好,但我会尽力提出我的问题所以每个人都能理解它。 :)



我的问题:



在我的公司,我们有几个流程从一个服务。大多数进程都会抛出Trace消息,因此我们可以看到程序正在做什么。

现在我想抓住这个Traces。

我编写了一个与开源程序DebugViewer类似的程序。如果进程已启动,则本地一切正常,我的程序可以捕获消息。

但是当我从服务启动进程时,我的DebugViewer无法再捕获消息。



也许有人为我提供解决方案:)



感谢您的帮助。



P.S.可能需要几天时间才能回复。 :)



亲切的问候BaronEK:)

解决方案

你从你开始单独的流程这一事实Windows服务进程看起来非常值得怀疑。单独的流程非常孤立,因此很难控制。我猜这种架构的原因之一是独立调试的可能性(我可以猜测你是从你的问题的上下文中做到的)。我会理解它但是...有很多更好的方法。



但这不是一个要点。请看我对这个问题的评论。如果您无法修改旨在由DebugViewer或类似工具使用的跟踪,则无法对其进行太多操作。如果您可以修改此跟踪代码,则应该执行此操作。其中一种方法是使用类 System.Diagnostics.EventLog 代替:

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx [ ^ ]。



这是一种非常通用的日志记录方式,适用于托管代码的任何方式,包括服务主机,这是一种非常特殊的方式。它也非常可靠。

这样,您可以将日志定向到不同的接收器,具体取决于您的配置,但默认情况下,如果将转到系统事件查看器小程序可查看的系统日志:eventvwr.msc。

你也可以创建自己的观众。你可以简单地将日志重定向到一个文件,或者任何东西。



如果你使用系统事件日志,你也可以隔离来自你公司产品的日志'将产品集合到一棵单独的树上。请查看我过去对此主题的回答:

如何登录单独的树结构:如何在文件夹下创建事件日志 [ ^ ],

如何制作自定义记录器: Windows中的MsBuild OutPut到TextBox申请 [ ^ ]。



-SA


< blockquote> Hallo Sergey Alexandrovich,

首先,谢谢你的评论。

经过网上的一些研究后,我找到了一种获取跟踪信息的方法就像我一样通缉。

我现在使用kernel32.dll创建DBWIN_Buffer和DBWIN_DATA事件并捕获其他应用程序的痕迹按摩。

kernel32的CreateEvent方法渴望一个名称。所以我可以传递DBWIN_BUFFER_READY和DBWIN_DATA_READY这两个字来创建两个事件。默认情况下,它在两个单词后面设置一个本地,因此该方法将其作为Local\DBWIN_BUFFER_READY进行交互,当我将其更改为Global\DBWIN_BUFFER_READY时,我有可能捕获服务的痕迹。

这是一个很有帮助的链接。也许你想看看它。



http://unixwiz.net/techtips/outputdebugstring.html



还是谢谢你的支持评论。如果我找不到其他可能性,这就是我想要的方式。 :)



这是我的代码的主要部分:



 



  //  为插槽创建事件'BUFFER_READY' 
p_brEvent = CreateEvent( ref sa, false true Global \\ \\\DBWIN_BUFFER_READY);
if (p_brEvent == IntPtr .Zero)
{
throw new CreateEventException( 无法创建事件(DBWIN_BUFFER_READY));
}

// 为插槽'DATA_READY'创建事件
p_drEvent = CreateEvent( ref sa, false false Global \\DBWIN_DATA_READY);

if (p_drEvent == IntPtr .Zero)
{
throw new CreateEventException( 无法创建事件DBWIN_DATA_READY);
}

// 获取插槽处可读共享内存的句柄' DBWIN_BUFFER'
// 创建一个Object并将其名称写入文件系统,所以其他进程可以使用它。
p_sfEvent = CreateFileMapping( new IntPtr (-1), ref sa,PageProtection.ReadWrite, 0 4096 Global \\DBWIN_BUFFER);
if (p_sfEvent == IntPtr .Zero)
{
throw new CreateEventException( 无法创建到插槽DBWIN_BUFFER的文件映射);
}


// 包含调试消息的文件将我映射到了virtuel shared
// 进程的内存,因此这两个进程都可以工作直接在内存中
map_shEvent = MapViewOfFile(p_sfEvent,SECTION_MAP_READ, 0 0 512 );
if (map_shEvent == IntPtr .Zero)
{
throw new CreateEventException( 无法创建到插槽DBWIN_BUFFER的文件映射);
}


Hallo everybody,
my english is not so good as it should be, but i will try to formulate my question as good as possible so everybody can understand it. :)

My question:

In my firm, we have several process which are lunched from one service. Most of the process throw Trace messages,so we have the possible to see, what the programm is doing.
Now I want to capture this Traces.
I have written a program which is similar too the open source program DebugViewer. If the processes are started local everything works fine and my program can capture the messages.
But when I start the process from a service my DebugViewer can not capture the messages any more.

Maybe somebody has a solution for me :)

Thank you for every help.

P.S. It maybe will takes several days until i will answer back. :)

Kind regards BaronEK :)

解决方案

The mere fact that you start separate processes from you Windows Service processes looks very questionable. Separate processes are very well isolated, so they are hard to control. I can guess that one of the reasons for such architecture is the possibilities for independent debugging (I can guess that you do it, from the context of your question). I would understand it but… there are much, much better ways.

But this is not a main point. Please see my comment to the question. If you cannot modify the tracing designed to be used by DebugViewer or similar facility, you cannot do much about it. If you can modify this tracing code, you should do it. One of the way is using the class System.Diagnostics.EventLog instead:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

This is the very universal way of logging suitable to any way you host the code, including the service host, which is a very special way. It is also very dependable.
This way, you can direct your logs to different sinks, depending on your configuration, but by default if will go to the system log viewable by the system "Event Viewer" applet: eventvwr.msc.
You also can create your own viewers. You can simply redirect logs to a file, or anything at all.

If you use the system event log, you can also isolate log coming from you product of company''s set of products to a separate tree. Please see my past answers on this topic:
how to log in a separate tree structure: How to create event log under a folder[^],
how to make a custom logger: MsBuild OutPut to the TextBox on the fly in Windows Application[^].

—SA


Hallo Sergey Alexandrovich,
First at all thank you for your comment.
After some more researchs on the web, I have found a way to get my trace messages such as I wanted.
I am now using the kernel32.dll to create DBWIN_Buffer and DBWIN_DATA events and to capture the traces massages of other applications.
The method CreateEvent of kernel32 longs for a name. So I can pass the words "DBWIN_BUFFER_READY" and "DBWIN_DATA_READY" to create two events. Defaultly it sets a "local" behind the two words, so the method interptrets it as "Local\DBWIN_BUFFER_READY", when i change it to "Global\DBWIN_BUFFER_READY" I´m getting the possibility to capture the traces of an service.
Here is a link which help me a lot. Maybe you wanna look at it.

http://unixwiz.net/techtips/outputdebugstring.html

Still thank you for your comment. It was the way I wanted to go if I would not find an other possibility. :)

Here is the main block of my code:


//Creates the Event for slot 'BUFFER_READY'
       p_brEvent = CreateEvent(ref sa, false, true, "Global\\DBWIN_BUFFER_READY");
           if (p_brEvent == IntPtr.Zero)
           {
               throw new CreateEventException("Failed to create event (DBWIN_BUFFER_READY)");
           }

       //Cretes the Event for slot 'DATA_READY'
           p_drEvent = CreateEvent(ref sa, false, false, "Global\\DBWIN_DATA_READY");

       if (p_drEvent == IntPtr.Zero)
       {
           throw new CreateEventException("Failed to create event DBWIN_DATA_READY");
       }

       // Get a handle to the readable shared memory at slot 'DBWIN_BUFFER'
       // Creates an Object and writes the name of it in the file system, so other processes can work with it.
       p_sfEvent = CreateFileMapping(new IntPtr(-1), ref sa, PageProtection.ReadWrite, 0, 4096, "Global\\DBWIN_BUFFER");
       if (p_sfEvent == IntPtr.Zero)
       {
           throw new CreateEventException("Failed to create a file mapping to slot DBWIN_BUFFER");
       }


       //File which contains the Debug Messages will me mapped into the virtuel shared
       //memory of the processes, so both processes can work with it directly in the memory
       map_shEvent = MapViewOfFile(p_sfEvent, SECTION_MAP_READ, 0, 0, 512);
       if (map_shEvent == IntPtr.Zero)
       {
           throw new CreateEventException("Failed to create a file mapping to slot DBWIN_BUFFER");
       }


这篇关于捕获服务进程的跟踪消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆