本文介绍了如何与进程名System.TraceListener prePEND消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找使用System.Diagnostics.Trace做记录是一个非常基本的应用程序。一般来说,做了所有我需要做的。缺点是,如果我叫

I have been looking at using System.Diagnostics.Trace for doing logging is a very basic app. Generally it does all I need it to do. The downside is that if I call

Trace.TraceInformation("Some info");

输出为SomeApp.Exe信息:0:一些信息。 Initally这种招待我,但不再。我想只输出一些信息,以安慰。因此,我认为写一个cusom TraceListener的,而不是使用内置的ConsoleTraceListener,将解决这个问题。我可以看到,我想第二个冒号后面的所有文字特定的格式。这里是我的尝试,看是否是可行的。

The output is "SomeApp.Exe Information: 0: Some info". Initally this entertained me but no longer. I would like to just output "Some info" to console. So I thought writing a cusom TraceListener, rather than using the inbuilt ConsoleTraceListener, would solve the problem. I can see a specific format that I want all the text after the second colon. Here is my attempt to see if this would work.

class LogTraceListener : TraceListener
{
    public override void Write(string message)
    {
        int firstColon = message.IndexOf(":");
        int secondColon = message.IndexOf(":", firstColon + 1);

        Console.Write(message);
    }

    public override void WriteLine(string message)
    {
        int firstColon = message.IndexOf(":");
        int secondColon = message.IndexOf(":", firstColon + 1);

        Console.WriteLine(message);
    }
}

如果我的输出firstColon的值时,它始终是-1。如果我把一个破发点的消息始终只是一些信息。哪里来的所有其它信息的呢?

If I output the value of firstColon it is always -1. If I put a break point the message is always just "Some info". Where does all the other information come from?

所以我必须看看在Console.WriteLine调用之前该点的调用堆栈。那个叫我WriteLine方法的方法是:System.dll中System.Diagnostics.TraceListener.TraceEvent(System.Diagnostics.TraceEventCache eventCache,串源,System.Diagnostics.TraceEventType事件类型,INT ID,字符串消息)+ 0x33字节

So I had a look at the call stack at the point just before Console.WriteLine was called. The method that called my WriteLine method is: System.dll!System.Diagnostics.TraceListener.TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string message) + 0x33 bytes

当我使用反射来看看这个消息,这一切似乎pretty的直线前进。我看不到任何code,它改变了字符串的值,我已经把它交给Console.WriteLine之后。可能posibly改变底层字符串值的唯一方法是调用UnsafeNativeMethods.EventWriteString其中有一个参数是指向该消息。

When I use Reflector to look at this message it all seems pretty straight forward. I can't see any code that changes the value of the string after I have sent it to Console.WriteLine. The only method that could posibly change the underlying string value is a call to UnsafeNativeMethods.EventWriteString which has a parameter that is a pointer to the message.

有谁明白这里究竟是怎么回事,我是否可以改变输出只是我的消息了额​​外的绒毛。这似乎是邪恶的魔法,我可以通过一个字符串一些信息,以Console.WriteLine(或任何其他方法为此事)和字符串输出是不同的。

Does anyone understand what is going on here and whether I can change the output to be just my message with out the additional fluff. It seems like evil magic that I can pass a string "Some info" to Console.WriteLine (or any other method for that matter) and the string that output is different.

编辑:我发现了魔法。显然,这不是魔术。 Write方法得到打来的电话呼叫的WriteLine这是我认为魔术是发生之前调用WriteHeader。

I found the magic. Obviously it wasn't magic. The Write method gets call from a call to WriteHeader before the call to WriteLine which is where I thought the magic was happening.

推荐答案

您可以通过覆盖TraceEvent()方法,在你的听众得到这个。像这样的:

You can get this by overriding the TraceEvent() method in your listener. Like this:

    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) {
        Console.WriteLine("{0}: {1}", id, message);
    }

这篇关于如何与进程名System.TraceListener prePEND消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:38