|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- namespace SafeCampus.Core.Extension
- {
- /// <summary>
- /// 日志帮助类
- /// </summary>
- public class LogHelper
- {
- /// <summary>
- /// 配置文件
- /// </summary>
- protected static LogConfig Config = new LogConfig();
-
- /// <summary>
- /// 写日志
- /// </summary>
- /// <param name="isError"></param>
- /// <returns></returns>
- protected static StreamWriterLock GetLogWriter(bool isError = false)
- {
- try
- {
- string root = AppDomain.CurrentDomain.BaseDirectory;
- DateTime now = DateTime.Now;
- //日志文件路径
- string folder = isError ? Config.LogErrorFolder : Config.LogInfoFolder;
- folder = Path.Combine(root, folder, now.ToString(Config.LogFolderFormat));
-
- string filePath = Path.Combine(folder, now.ToString(Config.LogFileFormat) + Config.LogFileExt);
-
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- return new StreamWriterLock(filePath);
- }
- catch (Exception)
- {
- return null;
- }
- }
-
- /// <summary>
- /// 写日志
- /// </summary>
- public static void WriteToLog(string content)
- {
- using (var sw = GetLogWriter())
- {
- var writer = sw.Writer;
- writer.WriteStart();
- writer.WriteTime();
- writer.WriteContent(content);
- writer.WriteLine();
- }
- }
-
- /// <summary>
- /// 写日志
- /// </summary>
- public static void WriteToLog(string[] contents)
- {
- using (var sw = GetLogWriter())
- {
- var writer = sw.Writer;
- writer.WriteStart();
- writer.WriteTime();
- writer.WriteContent(contents);
- writer.WriteEnd();
- }
- }
-
- /// <summary>
- /// 写日志
- /// </summary>
- public static void WriteToLog(string title, string content)
- {
- using (var sw = GetLogWriter())
- {
- var writer = sw.Writer;
- writer.WriteStart();
- writer.WriteTitle(title);
- writer.WriteTime();
- writer.WriteContent(content);
- writer.WriteEnd();
- }
- }
-
- /// <summary>
- /// 写日志(错误)
- /// </summary>
- public static void WriteToLog(Exception ex, string title = "", string content = "")
- {
- using (var sw = GetLogWriter(true))
- {
- var writer = sw.Writer;
- writer.WriteStart();
- writer.WriteTitle(title, true);
- writer.WriteTime();
- if (!string.IsNullOrEmpty(content))
- {
- writer.WriteContent(content);
- }
- writer.WriteException(ex);
- writer.WriteEnd();
- }
- }
- }
-
- /// <summary>
- /// 日志目录配置
- /// </summary>
- public class LogConfig
- {
- /// <summary>
- /// 构造函数
- /// </summary>
- public LogConfig()
- {
- this.LogRoot = "Log";
- this.LogInfoFolder = Path.Combine(this.LogRoot, "LogInfo");
- this.LogErrorFolder = Path.Combine(this.LogRoot, "LogError");
- this.LogFolderFormat = "yyyyMMdd";
- this.LogFileFormat = "yyyyMMdd_HH";
- this.LogFileExt = ".log";
- }
-
- /// <summary>
- /// 日志根目录
- /// </summary>
- public string LogRoot { get; set; }
-
- /// <summary>
- /// 普通信息
- /// </summary>
- public string LogInfoFolder { get; set; }
-
- /// <summary>
- /// 错误信息
- /// </summary>
- public string LogErrorFolder { get; set; }
-
- /// <summary>
- /// 文件夹日期格式
- /// </summary>
- public string LogFolderFormat { get; set; }
-
- /// <summary>
- /// 文件日志格式
- /// </summary>
- public string LogFileFormat { get; set; }
-
- /// <summary>
- /// 文件后缀名
- /// </summary>
- public string LogFileExt { get; set; }
- }
-
- /// <summary>
- /// 日志工具
- /// </summary>
- public static class LogTools
- {
- /// <summary>
- /// 开始写日志
- /// </summary>
- /// <param name="writer"></param>
- public static void WriteStart(this StreamWriter writer)
- {
- writer.WriteLine("-------------------------------------------------------------------------------------");
- }
-
- /// <summary>
- /// 结束写
- /// </summary>
- /// <param name="writer"></param>
- public static void WriteEnd(this StreamWriter writer)
- {
- //writer.WriteLine("-------------------------------------------------------------------------------------");
- writer.WriteLine();
- }
-
- /// <summary>
- /// 写入时间
- /// </summary>
- /// <param name="writer"></param>
- public static void WriteTime(this StreamWriter writer)
- {
- writer.WriteLine("Date:{0:yyyy-MM-dd} Time:{0:HH:mm:ss}", DateTime.Now);
- }
-
- /// <summary>
- /// 写入标题
- /// </summary>
- /// <param name="writer"></param>
- /// <param name="title"></param>
- /// <param name="isError"></param>
- public static void WriteTitle(this StreamWriter writer, string title, bool isError = false)
- {
- if (isError)
- {
- writer.WriteLine("Title:Error!!!");
- if (!string.IsNullOrEmpty(title))
- {
- writer.WriteLine(title);
- }
- }
- else
- {
- writer.WriteLine("Title:{0}", title);
- }
- }
-
- /// <summary>
- /// 写入内容
- /// </summary>
- /// <param name="writer"></param>
- /// <param name="content"></param>
- public static void WriteContent(this StreamWriter writer, string content)
- {
- writer.WriteLine("Content:{0}", content);
- }
-
- /// <summary>
- /// 写入内容
- /// </summary>
- /// <param name="writer"></param>
- /// <param name="contents"></param>
- public static void WriteContent(this StreamWriter writer, string[] contents)
- {
- writer.WriteLine("Content:{0}", contents.Length);
- var dig = Math.Max(2, contents.Length.ToString().Length); //位数
- for (int i = 0; i < contents.Length; i++)
- {
- writer.WriteLine("No.{0}->{1}", (i + 1).ToString().PadLeft(2, '0'), contents[i]);
- }
- }
-
- /// <summary>
- /// 写入异常
- /// </summary>
- /// <param name="writer"></param>
- /// <param name="ex"></param>
- public static void WriteException(this StreamWriter writer, Exception ex)
- {
- writer.WriteLine("Message:{0}", ex.Message);
- writer.WriteLine("StackTrace:{0}", ex.StackTrace);
- if (ex.InnerException != null)
- {
- writer.WriteLine("InnerException:");
- writer.WriteLine("Message:{0}", ex.InnerException.Message);
- writer.WriteLine("StackTrace:{0}", ex.InnerException.StackTrace);
- }
- }
- }
-
- /// <summary>
- /// 文件锁
- /// </summary>
- public class StreamWriterLock : IDisposable
- {
- /// <summary>
- /// 写入
- /// </summary>
- public StreamWriter Writer { get; protected set; }
-
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="filePath"></param>
- public StreamWriterLock(string filePath)
- {
- Lock.EnterWriteLock();
- if (!File.Exists(filePath))//如果文件不存在
- {
- File.Create(filePath).Close();
- }
- this.Writer = File.AppendText(filePath);
- }
-
- /// <summary>
- /// 文件读写锁
- /// </summary>
- protected static ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
-
- public void Dispose()
- {
- this.Writer.Dispose();
- Lock.ExitWriteLock();
- }
- }
- }
|