【IT168 技术文档】
以下代码为跟踪的方法
using System.Diagnostics;
namespace Legalsoft.Kernal.Utility
{
public class LogUtility
{
// 侦听器
private static TextWriterTraceListener listner = null;
// 输出跟踪文件
private const string TRACE_FILE = "c:\\trace.log";
// traceName为跟踪名,traceMessage为跟踪内容
public static void Trace(string traceName, string traceMessage)
{
// 变量定义
StringBuilder message = null;
// 登记侦听器
if (listner == null)
{
try
{
// 生成侦听器
listner = new TextWriterTraceListener(TRACE_FILE);
// 追加侦听器
System.Diagnostics.Trace.Listeners.Add(listner);
// 设置侦听器
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
catch (System.Exception)
{
return;
}
}
}
}
}
namespace Legalsoft.Kernal.Utility
{
public class LogUtility
{
// 侦听器
private static TextWriterTraceListener listner = null;
// 输出跟踪文件
private const string TRACE_FILE = "c:\\trace.log";
// traceName为跟踪名,traceMessage为跟踪内容
public static void Trace(string traceName, string traceMessage)
{
// 变量定义
StringBuilder message = null;
// 登记侦听器
if (listner == null)
{
try
{
// 生成侦听器
listner = new TextWriterTraceListener(TRACE_FILE);
// 追加侦听器
System.Diagnostics.Trace.Listeners.Add(listner);
// 设置侦听器
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
catch (System.Exception)
{
return;
}
}
}
}
}
在FORM中直接调用该方法即可
private void button1_Click(object sender, System.EventArgs e)
{
LogUtility.Trace( "button1_Click","进入了button1_Click事件" );
}
{
LogUtility.Trace( "button1_Click","进入了button1_Click事件" );
}
c:\trace.log文件中记录了所有代码的走向。