首页 新闻 会员 周边

关于System.Diagnostics.Trace类的问题

0
悬赏园豆:10 [已解决问题] 解决于 2010-08-04 22:24

/// /// The main entry point for the application. ///

[STAThread]

 public static void Main()

{

 try {

// setup generic exception handler...

SWF.Application.ThreadException += new ST.ThreadExceptionEventHandler(App.OnAppException);

// setup cleanup routine for normal app shutdown...

SWF.Application.ThreadExit += new System.EventHandler(App.OnAppExit);

// when app starts, delete the previous log file (if any) string logfile = System.AppDomain.CurrentDomain.BaseDirectory + "AppLog.txt"; System.IO.TextWriter log = new System.IO.StreamWriter(logfile);

 if (Globals.Trace) // then also send trace output to log file? {

System.Diagnostics.TextWriterTraceListener logger; logger = new System.Diagnostics.TextWriterTraceListener(log); System.Diagnostics.Trace.Listeners.Add(logger);

System.Diagnostics.Trace.Write("App starting: " + DateTime.Now); } SWF.Application.Run(new Form1()); }

catch(Exception ex)

{ OnAppException(null, new ST.ThreadExceptionEventArgs(ex)); }

 

以上是利用Trace类来记录程序运行日志的代码,可每次当应用程序重启后,AppLog.txt文件里以前的内容就被覆盖了。谁知道这个问题怎么解决呢?

JeffLiang的主页 JeffLiang | 初学一级 | 园豆:43
提问于:2010-08-03 22:25
< >
分享
最佳答案
0

将 System.IO.TextWriter log = new System.IO.StreamWriter(logfile);

这句修改为:System.IO.TextWriter log = new System.IO.StreamWriter(logfile,true);

StreamWriter构造函数有个参数 允许是覆盖、还是附加文本。StreamWriter(string path,bool append); 参数append 为 true 时表示"附加"。

收获园豆:10
HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-04 09:03
朋友,谢谢你的帮助。
JeffLiang | 园豆:43 (初学一级) | 2010-08-04 22:23
客气了, 相互学习。
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-06 10:25
其他回答(2)
0

可以把每次调试的日志写到一个单独的文件,以日期为名。

string logfile = System.AppDomain.CurrentDomain.BaseDirectory + "AppLog.txt";

在这名之前先建立一个日期为名的日志文件,然后代码后面向这个文件里写。

Astar | 园豆:40805 (高人七级) | 2010-08-04 08:03
0

楼上正解。

winzheng | 园豆:8797 (大侠五级) | 2010-08-04 09:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册