using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; namespace MvcApplication1.Common { public class LogHelper { public static void WriteError(Exception ex) { string path = HttpContext.Current.Server.MapPath("~/Logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".text"); FileStream fs; if (File.Exists(path)) { fs = new FileStream(path, FileMode.Append, FileAccess.Write); } else { fs = File.Create(path); } string errorinfo = "日期:" + DateTime.Now.ToString("hh:mm:ss") + "\r\n" + "摘要:" + ex.Message + "\r\n" + "详细信息:" + ex.StackTrace; byte[] bit = System.Text.Encoding.UTF8.GetBytes(errorinfo); fs.Write(bit, 0, bit.Length); fs.Close(); fs.Dispose(); } } }