using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace BLL
{
/// <summary>
/// 日志类
/// </summary>
/// <remarks>此日志类提供高性能的日志记录实现。
/// 当调用Write方法时不会造成线程阻塞,而是立即完成方法调用,因此调用线程不用等待日志写入文件之后才返回。</remarks>
public class Log : IDisposable
{
//日志对象的缓存队列
private static Queue<Msg> msgs;
//日志写入线程的控制标记
private static bool state;
//日志文件生命周期的时间标记
private static DateTime TimeSign;
/// <summary>
/// 创建日志对象的新实例,根据指定的日志文件路径和指定的日志文件创建类型
/// </summary>
/// <param name="p">日志文件保存路径</param>
/// <param name="t">日志文件创建方式的枚举</param>
public Log()
{
if (msgs == null)
{
state = true;
msgs = new Queue<Msg>();
Thread thread = new Thread(work);
thread.Start();
}
}
//日志文件写入线程执行的方法
private void work()
{
while (true)
{
//判断队列中是否存在待写入的日志
if (msgs.Count > 0)
{
Msg msg = null;
lock (msgs)
{
msg = msgs.Dequeue();
}
if (msg != null)
{
LogWrite(msg);
}
}
else
{
//判断是否已经发出终止日志并关闭的消息
if (state)
{
Thread.Sleep(1);
}
else
{
//无需关闭
}
}
}
}
//写入日志到表的方法
private void LogWrite(Msg msg)
{
try
{
//数据持久化
}
catch (Exception e)
{
Console.Out.Write(e);
}
}
/// <summary>
/// 写入新日志,根据指定的日志对象Msg
/// </summary>
/// <param name="msg">日志内容对象</param>
public void Write(Msg msg)
{
if (msg != null)
{
lock (msgs)
{
msgs.Enqueue(msg);
}
}
}
#region IDisposable 成员
/// <summary>
/// 销毁日志对象
/// </summary>
public void Dispose()
{
state = false;
}
#endregion
}
}
namespace BLL
{
/// <summary>
/// 表示一个日志记录的对象
/// </summary>
public class Msg
{
//日志记录的时间
private DateTime datetime;
//日志记录的类型
private MsgType type;
//用户ID
private string userId;
//模块ID
private int modular;
//项目ID
private int projectid;
//记录ID
private int modelid;
//原始模型
private object oldModel;
//新模型
private object newModel;
//自定义内容
//private string text = null;
//public string Text
//{
// get { return text; }
// set { text = value; }
//}
private string guid;
public string Guid
{
get { return guid; }
set { guid = value; }
}
private int batch;
public int Batch
{
get { return batch; }
set { batch = value; }
}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
//public Msg(MsgType p, string userId, int projectid, int modelid, int modular, string text)
// : this(DateTime.Now, p, null, null, userId, projectid, modelid, modular, text)
//{
//}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
public Msg(MsgType p, object oldModel, object newModel, string userId, string projectid, int modelid, int modular, string guid,int batch)
: this(DateTime.Now, p, oldModel, newModel, userId, Common.Toolbox.ConvertToInt32(projectid), modelid, modular, guid,batch)
{
}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
public Msg(MsgType p, object oldModel, object newModel, string userId, string projectid, int modelid, int modular)
: this(DateTime.Now, p, oldModel, newModel, userId, Common.Toolbox.ConvertToInt32(projectid), modelid, modular, "",0)
{
}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
public Msg(MsgType p, object oldModel, object newModel, string userId, int projectid, int modelid, int modular, string guid,int batch)
: this(DateTime.Now, p, oldModel, newModel, userId, projectid, modelid, modular, guid,batch)
{
}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
public Msg(MsgType p, object oldModel, object newModel, string userId, int projectid, int modelid, int modular)
: this(DateTime.Now, p, oldModel, newModel, userId, projectid, modelid, modular, "",0)
{
}
/// <summary>
/// 创建新的日志记录实例;
/// </summary>
/// <param name="dt">日志记录的时间</param>
/// <param name="t">日志记录的文本内容</param>
/// <param name="p">日志记录的消息类型</param>
/// <param name="userId">用户ID</param>
/// <param name="projectid">项目ID</param>
/// <param name="modelid">记录ID</param>
/// <param name="modular">模块ID</param>
/// <param name="table">表明称</param>
public Msg(DateTime dt, MsgType p, object oldModel, object newModel, string userId, int projectid, int modelid, int modular, string guid,int batch)
{
datetime = dt;
type = p;
this.oldModel = oldModel;
this.newModel = newModel;
this.userId = userId;
this.projectid = projectid;
this.modelid = modelid;
this.modular = modular;
this.guid = guid;
this.batch = batch;
}
public object OldModel
{
get { return oldModel; }
set { oldModel = value; }
}
public object NewModel
{
get { return newModel; }
set { newModel = value; }
}
public string UserId
{
get { return userId; }
set { userId = value; }
}
public int Modular
{
get { return modular; }
set { modular = value; }
}
public int Projectid
{
get { return projectid; }
set { projectid = value; }
}
public int Modelid
{
get { return modelid; }
set { modelid = value; }
}
/// <summary>
/// 获取或设置日志记录的时间
/// </summary>
public DateTime Datetime
{
get { return datetime; }
set { datetime = value; }
}
/// <summary>
/// 获取或设置日志记录的消息类型
/// </summary>
public MsgType Type
{
get { return type; }
set { type = value; }
}
}
}
namespace BLL
{
/// <summary>
/// 日志消息类型的枚举
/// string Name = Enum.Parse(typeof(Man), i.ToString()).ToString();
/// int j = Convert.ToInt32(Enum.Parse(typeof(Man), Name2));
/// enum Man
///{
/// 刘备 = 1,
/// 关羽 = 2,
/// 张飞 = 3
///}
/// </summary>
public enum MsgType
{
/// <summary>
/// 添加
/// </summary>
添加 = 1,
/// <summary>
/// 修改
/// </summary>
修改 = 2,
/// <summary>
/// 删除
/// </summary>
删除 = 3,
/// <summary>
/// 添加相似
/// </summary>
添加相似 = 4,
//查看 = 5,
//搜索 = 6,
//下载 = 7
}
}