本文共 1676 字,大约阅读时间需要 5 分钟。
1 、类 MailL og |
public class EmailLog { private static object _helper = new object (); private static EmailLog _instance; private static object _fileLock = new object (); private EmailL og() {} public static EmailLog GetInstance() { lock (_helper) { if (_instance == null ) { _instance = new EmailLog (); } } return _instance; } /// <summary> /// 发送Mail 日志 /// </summary> /// <param name="message"> 信息 </param> public void WriteEmailL og(string message) { string filePath = System.AppDomain .CurrentDomain.BaseDirectory + "mail.txt" ; StreamWriter sw = null ; FileStream fs = null ; lock (_fileLock) { if (!File .Exists(filePath)) { fs = System.IO.File .Create(filePath); sw = new StreamWriter (fs, Encoding .UTF8); sw.WriteLine("--------------------------------------------------------------------------" ); sw.WriteLine(message); sw.Flush(); sw.Close(); } else { fs = new FileStream (filePath, FileMode .Append); sw = new StreamWriter (fs, Encoding .UTF8); sw.WriteLine("--------------------------------------------------------------------------" ); sw.WriteLine(message); sw.Flush(); sw.Close(); } } } } |
2 、客户端代码 |
static void Main (string [] args) { EmailLog w1 = EmailLog .GetInstance(); w1.WriteEmailL og(" 发送Mail 给灵动生活..." ); EmailLog w2 = EmailLog .GetInstance(); w2.WriteEmailL og(" 发送Mail 给James Hao..." ); } |