using AllBase.Extension; using NaiBa.Util.Extension; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AllBase.Helper { public class LogHelper { private static object lockHelper = new object(); #region 写文本日志 /// /// 写日志 异步 /// 默认路径:根目录/Log/yyyy-MM/ /// 默认文件:yyyy-MM-dd.log /// /// 日志内容 自动附加时间 public static void Write(string logContent) { string logPath = DateTime.Now.ToString("yyyy-MM"); Write(logPath, logContent); } public static void WriteWithTime(string logContent) { string logPath = DateTime.Now.ToString("yyyy-MM"); logContent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + logContent; Write(logPath, logContent); } #endregion #region 写异常日志 /// /// 写异常日志 /// /// public static void Write(Exception ex) { string logContent = string.Empty; string logPath = DateTime.Now.ToString("yyyy-MM"); logContent += GetExceptionMessage(ex); Write(logPath, logContent); } public static void WriteWithTime(Exception ex, string messageExtend = "") { string logContent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine; string logPath = DateTime.Now.ToString("yyyy-MM"); logContent += GetExceptionMessage(ex, messageExtend); Write(logPath, logContent); } private static string GetExceptionMessage(Exception ex, string messageExtend = "") { string message = string.Empty; if (ex != null) { message += ex.Message; message += Environment.NewLine; if (!string.IsNullOrEmpty(messageExtend)) { message += messageExtend; message += Environment.NewLine; } Exception originalException = ex.GetOriginalException(); if (originalException != null) { if (originalException.Message != ex.Message) { message += originalException.Message; message += Environment.NewLine; } } message += ex.StackTrace; message += Environment.NewLine; } return message; } #endregion #region 写日志到指定路径 /// /// 写日志 异步 /// 默认文件:yyyy-MM-dd.log /// /// 日志目录[默认程序根目录\Log\下添加,故使用相对路径,如:营销任务] /// 日志内容 自动附加时间 public static void Write(string logPath, string logContent) { string logFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log"; Write(logPath, logFileName, logContent); } /// /// 写日志 异步 /// /// 日志目录 /// 日志文件名 /// 日志内容 自动附加时间 public static void Write(string logPath, string logFileName, string logContent) { if (string.IsNullOrWhiteSpace(logContent)) { return; } if (string.IsNullOrWhiteSpace(logPath)) { logPath = Path.Combine(FileHelper.GetApplicationDir(), NaiBaGlobalConfig.Log, DateTime.Now.ToString("yyyy-MM")); } else { logPath = Path.Combine(FileHelper.GetApplicationDir(), NaiBaGlobalConfig.Log, logPath.Trim('\\')); } if (string.IsNullOrWhiteSpace(logFileName)) { logFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log"; } if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } string fileName = Path.Combine(logPath, logFileName); Action taskAction = () => { lock (lockHelper) { using (StreamWriter sw = File.AppendText(fileName)) { sw.WriteLine(logContent + Environment.NewLine); sw.Flush(); sw.Close(); } } }; Task task = new Task(taskAction); task.Start(); } #endregion } }