

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

//一段简单的例子
Trace.WriteLine( "字符串匹配查找成功!");
//直接使用WriteLineIf进行判断
Trace.WriteLineIf( IsShowTrace,"起始位置是:" + myIndex.ToString());
//使用if语句进行判断
if ( true == IsShowTrace )
{
Trace.WriteLine( IsShowTrace,"起始位置是:" + myIndex.ToString());
}
// 演示如何使用Listener
using System;
using System.Diagnostics;
namespace Listener
{
class Application
{
[STAThread]
static void Main(string[] args)
{
Trace.WriteLine("Trace listener 测试");
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<remove type="System.Diagnostics.DefaultTraceListener"/>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener"
initializeData="myListener.log" />
<add name="myListenerEventLog" type="System.Diagnostics.EventLogTraceListener"
initializeData="Application" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
using System;
using System.Diagnostics;
namespace Switching
{
class SampleClass
{
// 建立一个switch,其值由config文件进行配置
static TraceSwitch generalSwitch = new TraceSwitch("CoolSwitch", "Global scope");
static public void SampleMethod()
{
//如果switch state设置为TraceError,输出该消息
Trace.WriteLineIf(generalSwitch.TraceError,"TraceError message");
//如果switch state设置为TraceWarning,输出该消息
Trace.WriteLineIf(generalSwitch.TraceWarning,"TraceWarning message");
//如果switch state设置为TraceInfo,输出该消息
Trace.WriteLineIf(generalSwitch.TraceInfo,"TraceInfo message");
//如果switch state设置为TraceVerbose,输出该消息
Trace.WriteLineIf(generalSwitch.TraceVerbose,"TraceVerbose message");
}
public static void Main(string[] args)
{
SampleMethod();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="CoolSwitch" value="2" />
</ switches >
</system.diagnostics>
</configuration>
TraceError message TraceWarning message
public bool FindCustomer (int customerID)
{
Debug.Assert (customerID > 0, "FindCustomer出错 ","customerID应该大于0");
// More code...
return true;
}
if ( SetCustomerValid(customerID))
{
Debug.Assert(CustomerIsValid(customerID));
UserCustomer(customerID);
}
else
{
Debug.Assert( !CustomerIsValid(customerID));
ReleaseCustomer(customerID);
}
[Conditional ("DEBUG")]
private void ImOK ()
{
Debug.Assert (this != null,"测试对象状态", "this 不能为 null");
// More here.
}
public bool SetCustomerValid (int customerID)
{
ImOK ();
Debug.Assert (customerID > 0, "FindCustomer出错 ","customerID应该大于0");
// More code...
return true;
}