2016-07-07 4 views
1

내가 가진 코드 :TraceListener 출력에서 ​​프로세스 이름을 제거하는 방법은 무엇입니까?

출력은 MyProgram.vshost.exe Information: 0 : Starting...DateTime=2016-07-07T10:43:10.5147858Z 방법 프로세스 이름을 인쇄하지 청취자에게 전하고있다
var listener = new TextWriterTraceListener(@"C:\logs\1.log", "myListener") 
     { 
      TraceOutputOptions = TraceOptions.DateTime 
     }; 

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

?

답변

0

불행히도 이것은 불가능한 것처럼 보입니다.

나는 그 리스너를 디 컴파일하여이 옵션이 없음을 발견했다. 그러나 아직 희망이 있습니다.

이 같은 자신의 리스너를 구현할 수 있습니다

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; 
     } 
    } 
0

당신은 (경우 TextWriterTraceListener에서 예를 들어) 사용자 지정 추적 수신기를 구현하고 당신이 기본 전에 필요로 대체 소스 매개 변수 값을 가진 모든 TraceData 메서드를 재정의해야 클래스 메소드 호출 (프로세스 (실행 파일) 이름 대신에 아무것도 표시 할 필요가 없으면 빈 문자열 만 전달하면 됨) 결과 트랜스 리스너 클래스는 다음과 같을 수 있습니다.

관련 문제