C#读取写入日志文本LogHelper类库
一个高效便捷的操作日志文件的帮助类库,将录入的内容写入文件,
可自定义生成规则和保存路径
在记录日志时,可以方便的调用
使用方法:
FileLogHelper.logContent.Add("日志内容");
FileLogHelper.WriterToText();
02 | using System.Collections.Generic; |
06 | using System.Configuration; |
11 | /// 使用方法: FileLogHelper.logContent.Add("日志内容");FileLogHelper.WriterToText(); |
13 | public class FileLogHelper |
15 | static string LogPath = ConfigurationManager.AppSettings[ "LogPath" ].ToString(); |
16 | static string filePath = LogPath System.DateTime.Now.ToString( "yyyy-MM-dd" ) ".txt" ; |
17 | public static List< string > logContent = new List< string >(); |
21 | /// <param name="path"></param> |
22 | /// <param name="content"></param> |
23 | public static void WriterToText() |
26 | string content = ReaderText(); |
28 | FileStream myfs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); |
30 | StreamWriter mySw = new StreamWriter(myfs); |
31 | foreach ( string log in logContent) |
35 | mySw.WriteLine(content); |
45 | /// <param name="path"></param> |
46 | /// <returns></returns> |
47 | public static string ReaderText() |
49 | if (!Directory.Exists(LogPath)) |
50 | Directory.CreateDirectory(LogPath); |
52 | FileStream myfs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read); |
54 | StreamReader mySr = new StreamReader(myfs); |
56 | string content = mySr.ReadToEnd(); |
原文链接:C#读取写入日志文本LogHelper类库