从 Trace 输出中删除项目 Name.vshost.exe

Get rid of the Project Name.vshost.exe from the Trace ouput

我已经在我的控制台应用程序上定义了一个跟踪监听器,就像这样

ConsoleTraceListener = new ConsoleTraceListener();
AutoFlush = true;
Listeners.Add(traceListener);

然后几次 Trace.TraceInformation 调用我所有的源代码。

关键是当我在命令行上执行代码时,我可以在每个 Traces 上看到以下内容:

我怎样才能摆脱

MyNewSampleProject.vshost.exe Information: 0:.

我只是想知道我在 Trace 中设置的消息。

谢谢!

不幸的是,这似乎不可能。

我反编译了那个监听器,发现没有这个选项。 但还是有希望的。

您可以像这样实现自己的监听器:

  public class MyListener : ConsoleTraceListener {

   public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) {
        if (this.Filter != null && !base.Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
            return;
        this.WriteLine(message);
        this.WriteFooter(eventCache);
    }

    private bool IsEnabled(TraceOptions opts) {
        return (uint) (opts & this.TraceOutputOptions) > 0U;
    }

    private void WriteFooter(TraceEventCache eventCache) {
        if (eventCache == null)
            return;
        this.IndentLevel = this.IndentLevel + 1;
        if (this.IsEnabled(TraceOptions.ProcessId))
            this.WriteLine("ProcessId=" + (object) eventCache.ProcessId);
        if (this.IsEnabled(TraceOptions.LogicalOperationStack)) {
            this.Write("LogicalOperationStack=");
            Stack logicalOperationStack = eventCache.LogicalOperationStack;
            bool flag = true;
            foreach (object obj in logicalOperationStack) {
                if (!flag)
                    this.Write(", ");
                else
                    flag = false;
                this.Write(obj.ToString());
            }
            this.WriteLine(string.Empty);
        }
        if (this.IsEnabled(TraceOptions.ThreadId))
            this.WriteLine("ThreadId=" + eventCache.ThreadId);
        if (this.IsEnabled(TraceOptions.DateTime))
            this.WriteLine("DateTime=" + eventCache.DateTime.ToString("o", (IFormatProvider) CultureInfo.InvariantCulture));
        if (this.IsEnabled(TraceOptions.Timestamp))
            this.WriteLine("Timestamp=" + (object) eventCache.Timestamp);
        if (this.IsEnabled(TraceOptions.Callstack))
            this.WriteLine("Callstack=" + eventCache.Callstack);
        this.IndentLevel = this.IndentLevel - 1;
    }
}