namespace SafeCampus.Core.Extension
{
///
/// 日志帮助类
///
public class LogHelper
{
///
/// 配置文件
///
protected static LogConfig Config = new LogConfig();
///
/// 写日志
///
///
///
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;
}
}
///
/// 写日志
///
public static void WriteToLog(string content)
{
using (var sw = GetLogWriter())
{
var writer = sw.Writer;
writer.WriteStart();
writer.WriteTime();
writer.WriteContent(content);
writer.WriteLine();
}
}
///
/// 写日志
///
public static void WriteToLog(string[] contents)
{
using (var sw = GetLogWriter())
{
var writer = sw.Writer;
writer.WriteStart();
writer.WriteTime();
writer.WriteContent(contents);
writer.WriteEnd();
}
}
///
/// 写日志
///
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();
}
}
///
/// 写日志(错误)
///
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();
}
}
}
///
/// 日志目录配置
///
public class LogConfig
{
///
/// 构造函数
///
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";
}
///
/// 日志根目录
///
public string LogRoot { get; set; }
///
/// 普通信息
///
public string LogInfoFolder { get; set; }
///
/// 错误信息
///
public string LogErrorFolder { get; set; }
///
/// 文件夹日期格式
///
public string LogFolderFormat { get; set; }
///
/// 文件日志格式
///
public string LogFileFormat { get; set; }
///
/// 文件后缀名
///
public string LogFileExt { get; set; }
}
///
/// 日志工具
///
public static class LogTools
{
///
/// 开始写日志
///
///
public static void WriteStart(this StreamWriter writer)
{
writer.WriteLine("-------------------------------------------------------------------------------------");
}
///
/// 结束写
///
///
public static void WriteEnd(this StreamWriter writer)
{
//writer.WriteLine("-------------------------------------------------------------------------------------");
writer.WriteLine();
}
///
/// 写入时间
///
///
public static void WriteTime(this StreamWriter writer)
{
writer.WriteLine("Date:{0:yyyy-MM-dd} Time:{0:HH:mm:ss}", DateTime.Now);
}
///
/// 写入标题
///
///
///
///
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);
}
}
///
/// 写入内容
///
///
///
public static void WriteContent(this StreamWriter writer, string content)
{
writer.WriteLine("Content:{0}", content);
}
///
/// 写入内容
///
///
///
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]);
}
}
///
/// 写入异常
///
///
///
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);
}
}
}
///
/// 文件锁
///
public class StreamWriterLock : IDisposable
{
///
/// 写入
///
public StreamWriter Writer { get; protected set; }
///
/// 构造函数
///
///
public StreamWriterLock(string filePath)
{
Lock.EnterWriteLock();
if (!File.Exists(filePath))//如果文件不存在
{
File.Create(filePath).Close();
}
this.Writer = File.AppendText(filePath);
}
///
/// 文件读写锁
///
protected static ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
public void Dispose()
{
this.Writer.Dispose();
Lock.ExitWriteLock();
}
}
}