using System; using System.IO; /// <summary> /// 打印error类 /// </summary> public class LogUtil { private string path = string.Empty; public LogUtil(string filePath, Enviroment enviroment) { switch (enviroment) { case Enviroment.HTTP: path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/") + filePath; break; case Enviroment.CLIENT: path = Directory.GetCurrentDirectory() + "/" + filePath; break; default: break; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } /// <summary> /// 日志写入 /// </summary> /// <param name="str">要写入的字符串</param> /// <param name="isAppend">是否是文本追加</param> public void LogError(string str, bool isAppend = true) { StreamWriter fs = new StreamWriter(path + "/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", isAppend); fs.WriteLine(DateTime.Now.ToString() + "---------" + str); fs.Close(); } /// <summary> /// 程序运行环境 /// </summary> public enum Enviroment { /// <summary> /// webapi环境 /// </summary> HTTP, /// <summary> /// 客户端 /// </summary> CLIENT } }