使用System.Diagnostics.Trace.WriteLine 将日志写入到文件中案例
1、app.config文件中,配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="switch" value="4"/>
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="listener" type="System.Diagnostics.TextWriterTraceListener" initializeData="AppLog.txt"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>
2、注册Trace监听
System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(DateTime.Now.ToString("yyyyMMdd") + ".log"));
3、将日志写入文件中
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString());
System.Diagnostics.Trace.WriteLine("getRecord exception");
System.Diagnostics.Trace.WriteLine(es.Message);
4、效果如下,调式中Debug目录添加文件并且写入日志