1 public class LogWriter 2 { 3 public void WriteToLog(string source,string targetSite,string message, string logPath) 4 { 5 try 6 { 7 //此日志文件存在 8 if (File.Exists(logPath)) 9 { 10 FileInfo log = new FileInfo(logPath); 11 //获取文件大小 12 float size = (float)log.Length / (1024 * 1024); 13 if (size >= 1) 14 { 15 //清除日志文件 16 File.Delete(logPath); 17 } 18 } 19 20 StreamWriter logWriter = new StreamWriter(logPath, true, Encoding.Default); 21 logWriter.WriteLine("错误发生时间:"+DateTime.Now.ToString()); 22 logWriter.WriteLine("错误来源:"+source); 23 logWriter.WriteLine("错误方法:"+targetSite); 24 logWriter.WriteLine("错误消息:"+message+"\r\n"); 25 logWriter.Close(); 26 } 27 catch 28 { 29 30 } 31 } 32 }