using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CameraErrorCheck { /// /// 文件帮助类 /// public class MyFileLogHelper { /// /// 将信息写入文件 /// /// /// public static void WriteFire(LogInfoMo infoObj) { try { //获取程序所在目录 string pathStr = AppDomain.CurrentDomain.BaseDirectory + "log\\" + (string.IsNullOrEmpty(infoObj.path) ? "" : infoObj.path + "\\") + DateTime.Now.Year + "\\" + DateTime.Now.ToString("yyyyMM") + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; //判断文件夹是否存在 if (Directory.Exists(pathStr) == false) { //不存在文件夹则创建 Directory.CreateDirectory(pathStr); } string firePath = pathStr + infoObj.msgType.ToString() + DateTime.Now.ToString("yyyyMMddHH") + ".txt"; using (FileStream fs = new FileStream(firePath, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { string str = DateTime.Now + "\t" + infoObj.message; sw.WriteLine(str); } } } catch (Exception ex) { string str = DateTime.Now + "\t 文件写入异常:" + ex; } } } /// /// 记录信息数据模型 /// public class LogInfoMo { /// /// 信息保存路径,默认不单独保存 /// public string path { get; set; } /// /// 记录信息内容字符串 /// public string message { get; set; } /// /// 信息类型 /// public EnumLogMsgTypeEnum msgType { get; set; } } /// /// 日志信息类型枚举 /// public enum EnumLogMsgTypeEnum { /// /// 正常信息 /// Info = 0, /// /// 错误信息 /// Error = 1, /// /// 调试信息 /// Debug = 2, } }