110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
class Test
{
public static void Main(string[] args)
{
using (StreamWriter w = File.AppendText(@"c:\temp\log.txt"))
{
Log("Test1",w);
Log("Test2",w);
w.Close();
}
using (StreamReader r = File.OpenText(@"c:\temp\log.txt"))
{
Logmsg(r);
}
}
private static void Logmsg(StreamReader r)
{
String line;
while ((line = r.ReadLine()) != null)
{
Console.WriteLine(line);
}
r.Close();
}
private static void Log(string logMessage, TextWriter w)
{
w.Write("\r\n开始记录日志信息y:");
w.WriteLine("{0},{1}",DateTime.Now.ToLongTimeString(),DateTime.Now.ToLongDateString());
w.WriteLine(" :{0}",logMessage);
w.WriteLine("---------------------------------");
w.Flush();
}
}
static Program()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string strException = string.Format("{0}发生系统异常。\r\n{1}\r\n\r\n\r\n", DateTime.Now, e.ExceptionObject.ToString());
File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemException.log"), strException);
}