[C#]如何使用TraceListener實作類似Visual Studio的輸出視窗
/// <summary>
/// Initializes a new instance of the <see cref="ListBoxLogTraceListener"/> class.
/// </summary>
/// <param name="listBox">The list box.</param>
public ListBoxLogTraceListener(ListBox listBox)
{
m_ListBox = listBox;
}
/// <summary>
/// Writes the output to the OutputDebugString function and to the <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)"/> method, followed by a carriage return and line feed (
). /// </summary> /// <param name=“message”>The message to write to OutputDebugString and <see cref=“M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)”/>.</param> /// <PermissionSet> /// <IPermission class=“System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” version=“1” Unrestricted=“true”/> /// <IPermission class=“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” version=“1” Flags=“ControlEvidence”/> /// </PermissionSet> public override void WriteLine(string message) { if (!m_ListBox.Visible) return;
if (m_ListBox.InvokeRequired)
{
m_ListBox.BeginInvoke(new MethodInvoker(
delegate { WriteLine(message); }
));
return;
}
m_ListBox.Items.Add(string.Format("{0} {1}", DateTime.Now, message));
}
}</pre></div>
namespace WindowsFormsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); //Debug.Listeners.Add(new ListBoxLogTraceListener(listBox1)); Trace.Listeners.Add(new ListBoxLogTraceListener(listBox1)); }
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Debug msg...");
}
private void button2_Click(object sender, EventArgs e)
{
Trace.WriteLine("Trace msg...");
}
}
public class ListBoxLogTraceListener : DefaultTraceListener
{
private ListBox m_ListBox { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ListBoxLogTraceListener"/> class.
/// </summary>
/// <param name="listBox">The list box.</param>
public ListBoxLogTraceListener(ListBox listBox)
{
m_ListBox = listBox;
}
/// <summary>
/// Writes the output to the OutputDebugString function and to the <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)"/> method, followed by a carriage return and line feed (
). /// </summary> /// <param name=“message”>The message to write to OutputDebugString and <see cref=“M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)”/>.</param> /// <PermissionSet> /// <IPermission class=“System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” version=“1” Unrestricted=“true”/> /// <IPermission class=“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” version=“1” Flags=“ControlEvidence”/> /// </PermissionSet> public override void WriteLine(string message) { if (!m_ListBox.Visible) return;
if (m_ListBox.InvokeRequired)
{
m_ListBox.BeginInvoke(new MethodInvoker(
delegate { WriteLine(message); }
));
return;
}
m_ListBox.Items.Add(string.Format("{0} {1}", DateTime.Now, message));
}
}
}