C# 创建一个日志文件
public class LogHelper { private static string logFolder = AppDomain.CurrentDomain.BaseDirectory + "errorlog"; private static string logFile; /// <summary> /// 构造函数 /// </summary> public LogHelper() { try { logFile = Path.Combine(logFolder, (DateTime.Now.Date.ToString("yyyy-MM-dd") + ".Log")); if (!Directory.Exists(logFolder)) { Directory.CreateDirectory(logFolder); } if (!File.Exists(logFile)) { //日志不存在创建 using (StreamWriter write = File.CreateText(logFile)) { write.WriteLine("----------记录日志----------"); } } } catch { } } /// <summary> /// 追加一条信息 /// </summary> /// <param name="text"></param> public void Write(string text) { try { using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(string.Format("{0}>{1}", DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] "), text)); } } catch { } } /// <summary> /// 追加一行信息 /// </summary> /// <param name="text"></param> public void WriteLine(string text) { try { using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(string.Format("\r\n{0}>{1}", DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] "), text)); } } catch { } } }