2015-01-27 9 views
0

다른 프로그램에 대한 C# 스크립트를 테스트하는 프로그램이 있습니다. 스크립트가 Debug.Writeline 또는 Trace.Writeline 메서드를 사용할 수 있고 텍스트를 richtextbox에 쓸 수있을 때 여러 가지 다른 색의 텍스트가 필요합니다. 디버그는 주황색이고 Trace는 파랑입니다.Tracelistener의 추적 메시지와 디버그 메시지를 구분하십시오.

내가 스크립트가 실행 직후 나는

System.Diagnostics.Trace.Listeners.Add(new TextboxTraceListener(outputTxt, "ScriptTraceListener", Color.DarkBlue)); 
System.Diagnostics.Debug.Listeners.Add(new TextboxTraceListener(outputTxt, "ScriptDebugListener", Color.DarkOrange)); 

이 스크립트를 TextBoxTraceListener은

System.Diagnostics.Trace.Listeners.Remove("ScriptTraceListener"); 
System.Diagnostics.Debug.Listeners.Remove("ScriptDebugListener"); 

되어 실행 직전에

class TextboxTraceListener : TraceListener 
{ 

    private DebugRichTextbox output; 
    private Color textColor; 

    public TextboxTraceListener(DebugRichTextbox output) :this(output, "RichTextboxTraceListener", Color.Black) 
    { 
    } 

    public TextboxTraceListener(DebugRichTextbox output, string name, Color textColor) 
    { 
     this.Name = name; 
     this.output = output; 
     this.textColor = textColor; 
    } 


    public override void Write(string message) 
    { 
     Action write = delegate() { output.write(message, this.textColor); }; 
     if (output.InvokeRequired) 
     { 
      IAsyncResult result = output.BeginInvoke(write); 
      output.EndInvoke(result); 
     } 
     else 
     { 
      write(); 
     } 
    } 

    public override void WriteLine(string message) 
    { 
     Action writeLine = delegate() { output.writeLine(message, this.textColor); }; 
     if (output.InvokeRequired) 
     { 
      IAsyncResult result = output.BeginInvoke(writeLine); 
      output.EndInvoke(result); 
     } 
     else 
     { 
      writeLine(); 
     } 

    } 
} 

이 나타나는 것을를 Debug.Print 및 Trace.Writeline 메시지는 두 Listener에 의해 포착되고 메시지는 파란색과 오렌지색 richtextbox입니다. 메시지가 디버그 또는 TextBoxTraceListener의 추적에서 왔는지 알려주는 방법이 있습니까?

덕분에, Eoghan

+0

추적 및 디버그는 추적 수신기와 디버그 모드에서 모두 읽을 수 있지만 디버그 수신기는 해제 모드에서 읽을 수 없습니다. 당신은 아마도 당신을 위해 해결할 수 있습니다, 추적을 사용하여 텍스트를 작성하고 자신의 클래스에서 색상을 구별 할 수있는 사용자 정의 클래스 (디버그 및 추적)를 제공합니다. –

답변

0

나는이 함께했다. 멋지지는 않지만 작동하는 것처럼 보입니다. 방금이 클래스의 인스턴스 인 리스너를 하나 추가합니다. 누군가가 더 좋은 솔루션을 가지고 있다면 그것에 관심이있을 것입니다.

class TextboxTraceListener : TraceListener 
{ 
private DebugRichTextbox output; 
private Color debugColor; 
private Color traceColor; 

public TextboxTraceListener(DebugRichTextbox output) :this(output, "RichTextboxTraceListener", Color.Black, Color.Black) 
{ 
} 

public TextboxTraceListener(DebugRichTextbox output, string name, Color debugColor, Color traceColor) 
{ 
    this.Name = name; 
    this.output = output; 
    this.debugColor = debugColor; 
    this.traceColor = traceColor; 
} 


public override void Write(string message) 
{ 
    Color thisColor = Color.Black; 
    StackTrace trace = new StackTrace(); 
    StackFrame[] stackFrames = trace.GetFrames(); 
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("TRACE"))) 
    { 
     thisColor = traceColor; 
    } 
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("DEBUG"))) 
    { 
     thisColor = debugColor; 
    } 

    Action write = delegate() { output.write(message, thisColor); }; 
    if (output.InvokeRequired) 
    { 
     IAsyncResult result = output.BeginInvoke(write); 
     output.EndInvoke(result); 
    } 
    else 
    { 
     write(); 
    } 
} 

public override void WriteLine(string message) 
{ 

    Color thisColor = Color.Black; 
    StackTrace trace = new StackTrace(); 
    StackFrame[] stackFrames = trace.GetFrames(); 
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("TRACE"))) 
    { 
     thisColor = traceColor; 
    } 
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("DEBUG"))) 
    { 
     thisColor = debugColor; 
    } 

    Action writeLine = delegate() { output.writeLine(message, thisColor); }; 
    if (output.InvokeRequired) 
    { 
     IAsyncResult result = output.BeginInvoke(writeLine); 
     output.EndInvoke(result); 
    } 
    else 
    { 
     writeLine(); 
    } 

} 
} 
+0

Trace & Debug에 쓰고있는 응용 프로그램을 제어하지 못합니까? – MatthewMartin

+0

나는 응용 프로그램을 제어 할 수 있지만 응용 프로그램은 IDE를 사용하지 않는 C# 스크립트를 작성하기위한 UI입니다. Trace & Debug에 쓰고있는 스크립트입니다. 리스너를 첨부하고 끝내기 전에 제거합니다. 이 두 지점 사이에서 제어 할 수있는 코드 비트는 UI에 작성된 스크립트를 컴파일하고 해당 어셈블리에서 메서드를 호출하는 것입니다. – Eoghan