一、定義日誌類型 public enum LogType { Debug = 0, Error = 1, Info = 2, Warn = 3 } 二、添加靜態LogHelper 類 public static class LogHelper { public static void Write(s ...
一、定義日誌類型
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);
四、效果