| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace MvvmScaffoldFrame48.DLL.LogTools
- {
- public class TxtLog
- {
- #region 变量
- static public int min = 0;
- static public int max = 10;
- #endregion
- #region 实例
- static public StringBuilder _log = new StringBuilder();
- static TxtGenerate fio;
- #endregion
- #region 构造方法
- static TxtLog()
- {
- fio = new TxtGenerate();
- fio.OpenWriteFile("log.txt");
- }
- #endregion
- #region 静态方法
- static public void error(object str)
- {
- log("Erorr:" + str, 10);
- }
- /// <summary>
- /// log写入
- /// </summary>
- /// <param name="str"></param>
- static public void log(object str)
- {
- log(str, 0);
- }
- static public void log(string str)
- {
- log(str, 0);
- }
- /// <summary>
- /// log等级定制
- /// </summary>
- /// <param name="p"></param>
- /// <param name="level"></param>
- public static void log(object p, int level)
- {
- log(p.ToString(), level);
- }
- static public void log(string str, int level)
- {
- if (level < fio.LogLevel)
- {
- return;
- }
- if (str == null)
- {
- str = "null";
- }
- fio.WriteLine(DateTime.Now.ToString("O") + "->" + str + "\r\n");
- }
- /// <summary>
- /// 修改Log的限制等级0日志最多,10最少
- /// </summary>
- /// <param name="Loglevel"></param>
- public static void ChangeLogLevel(int Loglevel)
- {
- fio.LogLevel = Loglevel;
- }
- public static void LogSurPlusMemory()
- {
- PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
- var ramAvailable = ramCounter.NextValue();
- string ramAvaiableStr = string.Format("{0} MB", ramAvailable);
- log($"剩余物理内存:{ramAvaiableStr}");
- }
- #endregion
- }
- public class TxtGenerate
- {
- #region 变量
- private string LogFloderPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
- private double LogFileSize = 128 * Math.Pow(1024, 3);
- public int LogLevel = 5;
- #endregion
- #region 实例
- private FileStream fsr;
- private FileStream fsw;
- private StreamWriter sw;
- private StreamReader sr;
- #endregion
- #region 共有方法
- /// <summary>
- /// 创建用于读取文件行的文件流和StreamWriter对象
- /// </summary>
- /// <param name="file"></param>
- public void OpenReadFile(string file)
- {
- if (!File.Exists(file))
- File.Create(file).Close();
- fsr = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
- sr = new StreamReader(fsr);
- }
- /// <summary>
- /// 关闭读文件流
- /// </summary>
- public void CloseReadFile()
- {
- if (fsr != null)
- fsr.Close();
- }
- /// <summary>
- /// 创建用于向文件中追加行的文件流和StreamWriter对象
- /// </summary>
- /// <param name="file"></param>
- public void OpenWriteFile(string file)
- {
- int i = 1;
- while (true)
- {
- if (!Directory.Exists(LogFloderPath))//如果不存在就创建file文件夹
- {
- Directory.CreateDirectory(LogFloderPath);
- }
- if (!File.Exists(LogFloderPath + file)) // 如果文件不存在,先创建这个文件
- File.Create(LogFloderPath + file).Close();
- // 以追加模式打开这个文件
- fsw = new FileStream(LogFloderPath + file, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
- if (fsw.Length > LogFileSize)
- {
- file = "log" + i + ".txt";
- i++;
- continue;
- }
- break;
- }
- // 根据创建的FileStream对象来创建StreamWriter对象
- sw = new StreamWriter(fsw);
- }
- /// <summary>
- /// 关闭写文件流
- /// </summary>
- public void CloseWriteFile()
- {
- if (fsw != null)
- fsw.Close();
- }
- /// <summary>
- /// 从文件中读取一行
- /// </summary>
- /// <returns></returns>
- public string ReadLine()
- {
- if (sr.EndOfStream) // 如果文件流指针已经指向文件尾部,返回null
- return null;
- return sr.ReadLine();
- }
- /// <summary>
- /// 向文件中追加一行字符串
- /// </summary>
- /// <param name="s"></param>
- public void WriteLine(string s)
- {
- if (fsw.Length > LogFileSize)
- {
- OpenWriteFile("log.txt");
- }
- lock (sw)
- {
- sw.WriteLine(s);
- sw.Flush(); // 刷新写入缓冲区,使这一行对于读文件流可见
- }
- }
- /// <summary>
- /// 用于判断文件流指针是否位于文件尾部
- /// </summary>
- /// <returns></returns>
- public bool IsEof()
- {
- return sr.EndOfStream;
- }
- #endregion
- }
- }
|