这篇文章主要为大家详细介绍了利用c#实现LogHelp 日记分天记录,只记30天日记示例,具有一定的参考价值,可以用来参考一下。
感兴趣c# LogHelp 日记分天记录,只记30天日记的实现方法的小伙伴,下面一起跟随四海网的小编罗X来看看吧。<br>
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(-30).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 > 30 * 1000 * 1000)
{
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 > 30 * 1000 * 1000)
{
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 = 1;
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 == 1)
{
Thread td = new Thread(WriteLogThread);
td.Start();
}
}
private static void WriteLogThread()
{
while (LogMode == 1)
{
WriteLog();
Thread.Sleep(10);
}
}
private static void WriteLog()
{
if (_HandleCacheCr.Count <= 0) 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 != 1) 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(-30).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 > 30 * 1000 * 1000)
{
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; }
}
}
本文来自:http://www.q1010.com/194/16383-0.html
注:关于利用c#实现LogHelp 日记分天记录,只记30天日记示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:日记
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。