123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.IO;
- using System.Reflection;
- namespace LJLib.Tools.File
- {
- internal class Logger : ILogger
- {
- private string _path;
- private string _filename = "log.txt";
- private object _syncRoot = new object();
- public Logger()
- {
- _path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
- }
- public Logger(string path)
- {
- _path = path;
- }
- public Logger(string path, string filename)
- {
- _path = path;
- _filename = filename;
- }
- public virtual string Path
- {
- get { return _path; }
- }
- public virtual string FileName
- {
- get { return _filename; }
- set { _filename = value; }
- }
- public virtual string FullName
- {
- get { return _path + "\\" + _filename; }
- }
- public virtual void WriteLog(string msg)
- {
- try
- {
- FileInfo file = new FileInfo(FullName);
- if (file.Exists && file.Length > 10240000)
- {
- string tmpfile = FullName + ".tmp";
- using (FileStream fs = file.Open(FileMode.Open, FileAccess.Read))
- using (StreamReader reader = new StreamReader(fs))
- {
- fs.Seek(-1024000, SeekOrigin.End);
- reader.ReadLine();
- using (StreamWriter sw = System.IO.File.AppendText(tmpfile))
- {
- while (!reader.EndOfStream)
- {
- string strline = reader.ReadLine();
- sw.WriteLine(strline);
- }
- sw.Flush();
- sw.Close();
- }
- }
- file.Delete();
- System.IO.File.Move(tmpfile, FullName);
- }
- using (StreamWriter sw = System.IO.File.AppendText(FullName))
- {
- string rslt = "//====================================================================\r\n";
- rslt += "// 时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
- rslt += "//--------------------------------------------------------------------\r\n";
- rslt += "// " + msg.Replace("\r\n", "\r\n// ") + "\r\n";
- rslt += "//====================================================================\r\n\r\n";
- sw.Write(rslt);
- sw.Flush();
- sw.Close();
- }
- }
- catch (System.Exception ex)
- {
-
- }
- }
- public virtual void InsertLog(string msg)
- {
- try
- {
- using (FileStream fs = System.IO.File.Open(FullName, FileMode.OpenOrCreate))
- {
- StreamReader reader = new StreamReader(fs);
- string filestr = reader.ReadToEnd();
- filestr = DateTime.Now.ToString("yyyy-MM-dd HH:mm") + " " + msg + "\r\n" + filestr;
- fs.Seek(0, SeekOrigin.Begin);
- StreamWriter writer = new StreamWriter(fs);
- writer.Write(filestr);
- writer.Flush();
- }
- }
- catch (System.Exception ex)
- {
-
- }
- }
- public virtual bool IsSynchronized
- {
- get
- {
- return false;
- }
- }
- public virtual object SyncRoot
- {
- get
- {
- return _syncRoot;
- }
- }
- public static Logger Synchronized(Logger logger)
- {
- return new SyncLogger(logger);
- }
- }
- }
|