一、定义日志类型

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);

四、效果

   

 

版权声明:本文为hyf69原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hyf69/p/8656645.html