追踪/日志记录在客户遇到问题时非常有用。您并不总是能够进行调试,而且转储可能无法提供导致该点的完整视图。它绝对可以补充转储或调试。
您还可以打开和关闭它,甚至切换级别。
DebugView是一个捕获跟踪的好程序:
http://technet.microsoft.com/en-us/sysinternals/bb896647
追踪:
http://msdn.microsoft.com/en-us/library/3at424ac.aspx
例如,这是一个带有可切换级别的示例跟踪类:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
namespace Sample
{
public class Tracer
{
static Dictionary<string, TraceSwitch> s_switches = new Dictionary<string, TraceSwitch>();
static TraceSwitch s_switch = new TraceSwitch("trace", "Default tracing switch");
static object s_locker = new object();
private static TraceSwitch GetSwitch (string category)
{
if (!s_switches.ContainsKey (category))
{
lock (s_locker)
{
if (!s_switches.ContainsKey (category))
{
TraceSwitch traceSwitch = new TraceSwitch(category,
String.Format("Tracing switch for category '{0}'", category));
s_switches.Add (category, traceSwitch);
}
}
}
return s_switches[category];
}
public static void Output(string message)
{
WriteLine("None", TraceLevel.Info, message);
}
public static void OutputIf(bool condition, string message)
{
if (condition)
{
Output(message);
}
}
public static void Output(string format, params object[] args)
{
Debug.Assert(format != null);
Output(string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void OutputIf(bool condition, string format, params object[] args)
{
if (condition)
{
Output(format, args);
}
}
public static void Error(string message)
{
if (s_switch.TraceError)
{
WriteLine(String.Empty, TraceLevel.Error, message);
}
}
public static void Error(string category, string message)
{
if (GetSwitch(category).TraceError)
{
WriteLine(category, TraceLevel.Error, message);
}
}
public static void ErrorIf(bool condition, string message)
{
if (condition)
{
Error(message);
}
}
public static void ErrorIf(string category, bool condition, string message)
{
if (condition)
{
Error(category, message);
}
}
public static void Error(string format, params object[] args)
{
Debug.Assert(format != null);
Error(string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void Error(string category, string format, params object[] args)
{
Debug.Assert(format != null);
Error(category, string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void ErrorIf(bool condition, string format, params object[] args)
{
if (condition)
{
Error(format, args);
}
}
public static void ErrorIf(string category,
bool condition,
string format,
params object[] args)
{
if (condition)
{
Error(category, format, args);
}
}
public static void Warning(string message)
{
if (s_switch.TraceWarning)
{
WriteLine(String.Empty, TraceLevel.Warning, message);
}
}
public static void Warning(string category, string message)
{
if (GetSwitch(category).TraceWarning)
{
WriteLine(category, TraceLevel.Warning, message);
}
}
public static void WarningIf(bool condition, string message)
{
if (condition)
{
Warning(message);
}
}
public static void WarningIf(string category, bool condition, string message)
{
if (condition)
{
Warning(category, message);
}
}
public static void Warning(string format, params object[] args)
{
Debug.Assert(format != null);
Warning(string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void Warning(string category, string format, params object[] args)
{
Debug.Assert(format != null);
Warning(category, string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void WarningIf(bool condition, string format, params object[] args)
{
if (condition)
{
Warning(format, args);
}
}
public static void WarningIf(string category,
bool condition,
string format,
params object[] args)
{
if (condition)
{
Warning(category, format, args);
}
}
public static void Info(string message)
{
if (s_switch.TraceInfo)
{
WriteLine(String.Empty, TraceLevel.Info, message);
}
}
public static void Info(string category, string message)
{
if (GetSwitch(category).TraceInfo)
{
WriteLine(category, TraceLevel.Info, message);
}
}
public static void InfoIf(bool condition, string message)
{
if (condition)
{
Info(message);
}
}
public static void InfoIf(string category, bool condition, string message)
{
if (condition)
{
Info(category, message);
}
}
public static void Info(string format, params object[] args)
{
Debug.Assert(format != null);
Info(string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void Info(string category, string format, params object[] args)
{
Debug.Assert(format != null);
Info(category, string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void InfoIf(bool condition, string format, params object[] args)
{
if (condition)
{
Info(format, args);
}
}
public static void InfoIf(string category,
bool condition,
string format,
params object[] args)
{
if (condition)
{
Info(category, format, args);
}
}
public static void Verbose(string message)
{
try
{
if (s_switch.TraceVerbose)
{
WriteLine(String.Empty, TraceLevel.Verbose, message);
}
}catch{}
}
public static void Verbose(string category, string message)
{
if (GetSwitch(category).TraceVerbose)
{
WriteLine(category, TraceLevel.Verbose, message);
}
}
public static void VerboseIf(bool condition, string message)
{
if (condition)
{
Verbose(message);
}
}
public static void VerboseIf(string category, bool condition, string message)
{
if (condition)
{
Verbose(category, message);
}
}
public static void Verbose(string format, params object[] args)
{
Debug.Assert(format != null);
Verbose(string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void Verbose(string category, string format, params object[] args)
{
Debug.Assert(format != null);
Verbose(category, string.Format(CultureInfo.InvariantCulture, format, args));
}
public static void VerboseIf(bool condition, string format, params object[] args)
{
if (condition)
{
Verbose(format, args);
}
}
public static void VerboseIf(string category,
bool condition,
string format,
params object[] args)
{
if (condition)
{
Verbose(category, format, args);
}
}
private static void WriteLine(string category,
System.Diagnostics.TraceLevel level,
string message)
{
Debug.Assert(message != null);
string traceLine = string.Format(
CultureInfo.InvariantCulture,
"[{0}:{1}]{2}|{3}|{4:HH}:{4:mm}:{4:ss}.{4:fff}|{5}",
category,
level.ToString(),
Process.GetCurrentProcess().Id,
Thread.CurrentThread.ManagedThreadId,
DateTime.Now,
message);
Trace.WriteLine(traceLine);
}
}
}