自定义日志记录功能,按日记录,很方便
一、定义日志类型
public enum LogType
{
Debug = 0,
Error = 1,
Info = 2,
Warn = 3
}
二、添加静态LogHelper 类
public static class LogHelper
{
public static void Write(string msg, LogType logtype = LogType.Debug)
{
try
{
string basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
Directory.CreateDirectory(basePath);
string logPath = Path.Combine(basePath, DateTime.Today.ToString("yyyyMMdd") + ".log");
using (FileStream stream = new FileStream(logPath, FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
string messge = string.Format("{0} {1} - 操作人:【{2}】,Messge:{3}",
DateTime.Now.ToString(), logtype.ToString(), DeluxeIdentity.Current.User.DisplayName,msg);
writer.WriteLine(messge);
writer.Flush();
}
}
}
catch (Exception e)
{
throw new Exception( "日志写入异常:" + e.ToString());
}
}
}
三、调用方法
LogHelper.Write("检测拟单页面审批意见是否重复保存异常,原因:" + e.ToString(), LogType.Error);
四、效果