/// /// 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文件里以前的内容就被覆盖了。谁知道这个问题怎么解决呢?
将 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 时表示"附加"。
可以把每次调试的日志写到一个单独的文件,以日期为名。
string logfile = System.AppDomain.CurrentDomain.BaseDirectory + "AppLog.txt";
在这名之前先建立一个日期为名的日志文件,然后代码后面向这个文件里写。
楼上正解。