using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; namespace Share
{
public class LogHelp
{
private static object Block = new object();
#region 记录日志
/// <summary>
/// 记录日志
/// </summary>
/// <param name="msg"></param>
public static void WriteLog(string msg)
{ //string path = @"C:\log.txt";
lock (Block)
{
string filename = DateTime.Now.ToString("yyyyMMdd");
//该日志文件会存在windows服务程序目录下
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + filename + ".txt"; #region 删除前30天的日记
string sYue = DateTime.Now.AddDays(-).ToString("yyyyMMdd");
FileHelp.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + sYue + ".txt");
#endregion
if (!Directory.Exists(path))//如果不存在,则创建
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\");
} FileInfo file = new FileInfo(path);
if (!file.Exists)
{
FileStream fs;
fs = File.Create(path);
fs.Close();
}
else if (file.Length > * * )
{
file.Delete();
FileStream fs;
fs = File.Create(path);
fs.Close();
} using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + " " + msg);
}
}
}
}
public static void WriteLog2(string msg)
{ //string path = @"C:\log.txt";
lock (Block)
{
//该日志文件会存在windows服务程序目录下
string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
FileStream fs;
fs = File.Create(path);
fs.Close();
}
else if (file.Length > * * )
{
file.Delete();
FileStream fs;
fs = File.Create(path);
fs.Close();
} using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + " " + msg);
}
}
}
} #endregion
}
}
改进版,加入线程处理写日记
public class LogHelp
{
/// <summary>
/// 1为写日记,0为不写
/// </summary>
private static int LogMode = ;
private static object Block = new object(); /// <summary>
/// 处理缓存控制器
/// </summary>
private static Queue<LogMsg> _HandleCacheCr = new Queue<LogMsg>(); public static void Init(int logMode)
{
LogMode = logMode;
if (LogMode == )
{
Thread td = new Thread(WriteLogThread);
td.Start();
}
} private static void WriteLogThread()
{
while (LogMode == )
{
WriteLog();
Thread.Sleep();
}
} private static void WriteLog()
{
if (_HandleCacheCr.Count <= ) return;
LogMsg logMsg = _HandleCacheCr.Dequeue();
Write_Log(logMsg.Time.ToString("yyyy-MM-dd HH:mm:ss:ffff") + " " + logMsg.Msg);
} #region 记录日志
public static void WriteLog(string msg)
{
if (LogMode != ) return;
LogMsg logMsg = new LogMsg();
logMsg.Time = DateTime.Now;
logMsg.Msg = msg;
_HandleCacheCr.Enqueue(logMsg); }
/// <summary>
/// 记录日志
/// </summary>
/// <param name="msg"></param>
private static void Write_Log(string msg)
{ //string path = @"C:\log.txt";
lock (Block)
{
string filename = DateTime.Now.ToString("yyyyMMdd");
//该日志文件会存在windows服务程序目录下
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + filename + ".txt"; #region 删除前30天的日记
string sYue = DateTime.Now.AddDays(-).ToString("yyyyMMdd");
FileHelp.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + sYue + ".txt");
#endregion
if (!Directory.Exists(path))//如果不存在,则创建
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\");
} FileInfo file = new FileInfo(path);
if (!file.Exists)
{
FileStream fs;
fs = File.Create(path);
fs.Close();
}
else if (file.Length > * * )
{
file.Delete();
FileStream fs;
fs = File.Create(path);
fs.Close();
} using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine( msg);
}
}
}
} #endregion public class LogMsg
{
/// <summary>
/// 写日记的时间
/// </summary>
public DateTime Time { get; set; }
/// <summary>
/// 信息
/// </summary>
public string Msg { get; set; }
} }
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!