Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove process name from TraceListener output?

I have code:

 var listener = new TextWriterTraceListener(@"C:\logs\1.log", "myListener")
        {
            TraceOutputOptions = TraceOptions.DateTime
        };

        Trace.Listeners.Add(listener);
        Trace.TraceInformation("Starting...");

The output is MyProgram.vshost.exe Information: 0 : Starting...DateTime=2016-07-07T10:43:10.5147858Z How tell to listener to not print process name?

like image 768
Brans Ds Avatar asked Nov 23 '25 05:11

Brans Ds


2 Answers

Unfortunately this seems not possible.

I decompiled that listener and found, that there is no option to this. But there's still hope.

You can implement your own Listener like this:

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;
      }
    }
like image 165
lokusking Avatar answered Nov 24 '25 18:11

lokusking


You need to implement custom trace listener(for example from TextWriterTraceListener) and just override all TraceData methods with replaced source parameter value to what you need before base class method call(if nothing is need to be displayed instead of process(executable file) name - just pass empty string), so that result trance listener class may be like this:

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} ");
    }
}
like image 28
Volodymyr Avatar answered Nov 24 '25 20:11

Volodymyr