using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 心电图绘制 { /// /// 日志类 /// public sealed class LogHelper { public static int level = 3; public static string path = AppDomain.CurrentDomain.BaseDirectory + "\\Content\\Log\\"; /// /// 记录日志 /// /// /// public static void Info(string title, string content) { Task.Factory.StartNew(() => { new LogHelper().Log("info", title, content); }); } /// /// 记录调试日志 /// /// /// public static void Debug(string title, string content) { Task.Factory.StartNew(() => { new LogHelper().Log("debug", title, content); }); } /// /// 记录错误日志 /// /// /// public static void Error(string title, string content) { Task.Factory.StartNew(() => { new LogHelper().Log("error", title, content); }); } /// /// 记录异常日志 /// /// /// public static void Error(string type, Exception ex) { Task.Factory.StartNew(() => { new LogHelper().Log(type, ex); }); } /// /// 记录一般日志 /// /// /// /// private void Log(string type, string title, string content) { string name = string.Concat(DateTime.Now.ToString("yyyyMMdd"), ".TXT"); string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); string write_content = $"[{time}][{type}][{title}][{content}]"; using (FileStream fs = new FileStream(path + name, FileMode.Append, FileAccess.Write)) { using (StreamWriter w = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"))) { w.WriteLine(write_content); } } } /// /// 记录异常错误日志 /// /// /// private void Log(string type, Exception ex) { string name = string.Concat(DateTime.Now.ToString("yyyyMMdd"), ".error.config"); string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); using (FileStream fs = new FileStream(path + name, FileMode.Append, FileAccess.Write)) { using (StreamWriter w = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"))) { w.WriteLine("------------------------------------------------------------------"); w.WriteLine(string.Concat("[1.位置:", type, "]")); w.WriteLine(string.Concat("[2.时间:", time, "]")); w.WriteLine(string.Concat("[4.实例:", ex.InnerException, "]")); w.WriteLine(string.Concat("[5.来源:", ex.Source, "]")); w.WriteLine(string.Concat("[6.方法:", ex.TargetSite, "]")); w.WriteLine(string.Concat("[7.堆栈:", ex.StackTrace, "]")); w.WriteLine(string.Concat("[8.提示:", ex.Message, "]")); w.WriteLine("------------------------------------------------------------------"); } } } } }