using System; using System.IO; using System.Text; using System.Web; namespace RB_MES_API.Controllers { public static class LogHelper { private static readonly object writeFile = new object(); /// /// 在本地写入错误日志 /// /// public static void WriteLog(string debugstr) { lock (writeFile) { FileStream fs = null; StreamWriter sw = null; try { string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; //服务器中日志目录 string folder = MapPath("rblog"); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); fs = new FileStream(folder + "/" + filename, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs, Encoding.UTF8); sw.WriteLine(DateTime.Now.ToString() + " " + debugstr); } finally { if (sw != null) { sw.Dispose(); } if (fs != null) { fs.Dispose(); } } } } /// /// 在本地写入错误日志 /// /// public static void WriteLog(string debugstr,string name) { lock (writeFile) { FileStream fs = null; StreamWriter sw = null; try { string filename = DateTime.Now.ToString("yyyyMMdd") + name + ".txt"; //服务器中日志目录 string folder = MapPath("rblog"); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); fs = new FileStream(folder + "/" + filename, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs, Encoding.UTF8); sw.WriteLine(DateTime.Now.ToString() + " " + debugstr); } finally { if (sw != null) { sw.Dispose(); } if (fs != null) { fs.Dispose(); } } } } /// /// 获取文件路径 /// /// /// private static string MapPath(string strPath) { strPath = strPath.Replace("/", "\\"); if (strPath.StartsWith("\\")) { strPath = strPath.TrimStart('\\'); } return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } } }