如何从跟踪侦听器输出中删除进程名称

本文关键字:删除 进程名 输出 侦听器 跟踪 | 更新日期: 2023-09-27 17:55:48

我有代码:

 var listener = new TextWriterTraceListener(@"C:'logs'1.log", "myListener")
        {
            TraceOutputOptions = TraceOptions.DateTime
        };
        Trace.Listeners.Add(listener);
        Trace.TraceInformation("Starting...");

输出为MyProgram.vshost.exe Information: 0 : Starting...DateTime=2016-07-07T10:43:10.5147858Z如何告诉侦听器不打印进程名称?

如何从跟踪侦听器输出中删除进程名称

不幸的是,

这似乎是不可能的。

我反编译了那个听众,发现没有选择。但还是有希望的。

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

public class MyListener : TextWriterTraceListener {
      public MyListener(Stream s) : base(s) {
      }
      public MyListener(string s, string s1) : base(s, s1) {
      }
      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;
      }
    }
您需要

实现自定义跟踪侦听器(例如从 TextWriterTraceListener),并且只需将所有 TraceData 方法替换为基类方法调用之前所需的源参数值(如果不需要显示任何内容而不是进程(可执行文件)名称 - 只需传递空字符串),因此结果恍惚侦听器类可能是这样的:

public class MyTraceListener : TextWriterTraceListener
{
    public MyTraceListener()
    { }
    public MyTraceListener(string name) : base(name)
    { }
    public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
    {
        base.TraceData(eventCache, "", eventType, id, data);
    }
    public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
    {
        base.TraceData(eventCache, "", eventType, id, data);
    }
    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
    {
        base.TraceEvent(eventCache, "", eventType, id);
    }
    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
    {
        base.TraceEvent(eventCache, "", eventType, id, message);
    }
    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
    {
        base.TraceEvent(eventCache, "", eventType, id, format, args);
    }
    public override void Write(string message)
    {
        Writer.Write($"{message} ");
    }
    public override void WriteLine(string message)
    {
        Writer.WriteLine($"{DateTime.UtcNow:s}: {message} ");
    }
}