生成日志文件操作

c# 创建日志文件,非常的简单,这里也写一下记录吧!!!

直接上代码:

  1       #region   当前文件输出路径
  2         public static string SYSTEM_OUTLOG_FILEDIRECTORY = Directory.GetCurrentDirectory() + "/logfile/";
  3 
  4         #endregion
  5 
  6         #region   公有方法
  7 
  8         /// <summary>
  9         /// 给日志文件写入信息
 10         /// </summary>
 11         /// <param name="info">需要写入的信息</param>
 12         /// <returns>true:表示写入成功</returns>
 13         public static bool Write(string needWriteInfo)
 14         {
 15             bool success = false;
 16             if (File.Exists(LogPath))
 17             {
 18                 //追加文件
 19                 using (FileStream fs = new FileStream(LogPath, FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))
 20                 {
 21                     using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
 22                     {
 23                         //开始写入
 24                         sw.WriteLine(DateTime.Now + "   " + needWriteInfo);
 25                         success = true;
 26                     }
 27                 }
 28             }
 29             else
 30             {
 31                 using (FileStream fs = new FileStream(LogPath, FileMode.CreateNew, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
 32                 {
 33                     using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
 34                     {
 35                         //开始写入
 36                         sw.WriteLine(DateTime.Now + ": " + needWriteInfo);
 37                         success = true;
 38                     }
 39                 }
 40             }
 41 
 42             return success;
 43         }
 44 
 45         /// <summary>
 46         /// 读取日志文件的信息
 47         /// </summary>
 48         /// <param name="path">文件路径</param>
 49         /// <returns>返回读取的信息</returns>
 50         public static string Read(string path)
 51         {
 52             string contents = string.Empty;
 53             if (File.Exists(path))
 54             {
 55                 using (FileStream fs = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
 56                 {
 57                     using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
 58                     {
 59                         //开始读取
 60                         contents = sr.ReadToEnd();
 61                     }
 62                 }
 63             }
 64             return contents;
 65         }
 66 
 67         /// <summary>
 68         /// 清除日志文件的信息
 69         /// </summary>
 70         /// <param name="path">文件的路径</param>
 71         /// <returns>true:表示清空成功</returns>
 72         public static bool Clear(string path)
 73         {
 74             bool success = false;
 75             if (File.Exists(path))
 76             {
 77                 using (FileStream fs = new FileStream(path, FileMode.Open, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
 78                 {
 79                     fs.Seek(0, SeekOrigin.Begin);
 80                     fs.SetLength(0);
 81                     success = true;
 82                 }
 83             }
 84             return success;
 85         }
 86 
 87         /// <summary>
 88         /// 删除日志文件
 89         /// </summary>
 90         /// <param name="path">文件的路径</param>
 91         /// <returns>true:表示删除成功</returns>
 92         public static bool Delete(string path)
 93         {
 94             bool success = false;
 95             if (File.Exists(path))
 96             {
 97                 File.Delete(path);
 98                 success = true;
 99             }
100             return success;
101         }
102 
103         #endregion
104 
105         #region   私有方法
106 
107         /// <summary>
108         /// 日志文件存放路径
109         /// </summary>
110         private static string LogPath
111         {
112             get
113             {
114                 string OutPath = SYSTEM_OUTLOG_FILEDIRECTORY;
115 
116                 if (!Directory.Exists(OutPath))
117                 {
118                     Directory.CreateDirectory(OutPath);
119                 }
120 
121                 return OutPath + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
122             }
123         }
124 
125         #endregion

猜你喜欢

转载自www.cnblogs.com/zhao987/p/12566928.html