自定義日誌記錄功能,按日記錄,很方便

1、定義日誌類型日誌

public enum LogType
{
  Debug = 0,
  Error = 1,
  Info = 2,
  Warn = 3
}orm

2、添加靜態LogHelper 類blog

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());
    }
  }
}string

3、調用方法it

   LogHelper.Write("檢測擬單頁面審批意見是否重複保存異常,緣由:" + e.ToString(), LogType.Error);io

4、效果class

   

相關文章
相關標籤/搜索