一直在用log4net,最近也大致看了下里面的代码,总感觉配置文件复杂和格式太单一,于是最近自己封装了这个项目.功能当然比前面的相对单一,但是可以自定义样式和通过配置文件来设置Logger保存地址,现有Logger文件命名方法是DateTime.Now.Date.ToString("yyyyMMdd")+".log";
该项目主要有3个接口,ILogger实现记录日志,ILoggerStyle自定义日志样式,ILoggerEntity即日志实体,自己定该实体.
设计的方案是,
使用者自定义实体,把实体参给ILogger记录日志的方法,该方法记录实体的日志,而实体有2个必须实现属性,即定义在ILoggerEntity的属性 ILoggerStyle Style {get;set;} 和 string PathKey{get;set;}
第一个是该日志实体的样式,第二个为在配置文件中日志写入的路径信息.
该项目主要有2个Hander,分别实现IConfigurationSectionHandler,即可在配置文件中自定义节点,并且有两个工厂类,
两个类即通过配置文件来缓存日志样式和日志路径的.
使用主要是在配置文件自定义好日志样式和日志路径,系统会使用缓存保存,在自定义实体后,赋予样式和路径,再参往ILogger即写下日志.
上例为Logger的使用方法,多线程的安全测试是4线程测试的,线程安全问题大家可以放心。下面OrmEntity的实体,
该项目主要有3个接口,ILogger实现记录日志,ILoggerStyle自定义日志样式,ILoggerEntity即日志实体,自己定该实体.
设计的方案是,
使用者自定义实体,把实体参给ILogger记录日志的方法,该方法记录实体的日志,而实体有2个必须实现属性,即定义在ILoggerEntity的属性 ILoggerStyle Style {get;set;} 和 string PathKey{get;set;}
第一个是该日志实体的样式,第二个为在配置文件中日志写入的路径信息.
该项目主要有2个Hander,分别实现IConfigurationSectionHandler,即可在配置文件中自定义节点,并且有两个工厂类,
两个类即通过配置文件来缓存日志样式和日志路径的.
使用主要是在配置文件自定义好日志样式和日志路径,系统会使用缓存保存,在自定义实体后,赋予样式和路径,再参往ILogger即写下日志.
dll文件下载地址:
http://download.youkuaiyun.com/source/886850
下面看一个使用例子:
- public class LoggerTest
- {
- private ILoggerStyle style;
- public LoggerTest()
- {
- style = LoggerStyleFactory.GetLoggerStyle("orm");
- }
- public void Test()
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(start1), 1);
- ThreadPool.QueueUserWorkItem(new WaitCallback(start2), 2);
- }
- private void start1(object obj)
- {
- OrmEntity entity = new OrmEntity();
- entity.PathKey = "path1";
- entity.CmdText = "start0";
- entity.CmdType = System.Data.CommandType.Text;
- entity.ConnectionString = "LogInfo,在父目录";
- entity.ControlTime = DateTime.Now;
- entity.IsSuccess = true;
- entity.Style = style;
- for (int i = 20000; i < 40000; i++)
- {
- ILogger logger = new Logger.Logger();
- entity.CallBackObject = i;
- logger.LogInfo(entity);
- Console.WriteLine(i);
- }
- }
- private void start2(object obj)
- {
- OrmEntity entity = new OrmEntity();
- entity.PathKey = "path2";
- entity.CmdText = "start0";
- entity.CmdType = System.Data.CommandType.Text;
- entity.ConnectionString = "LogError,在子目录";
- entity.ControlTime = DateTime.Now;
- entity.IsSuccess = true;
- entity.Style = style;
- for (int i = 40000; i < 60000; i++)
- {
- ILogger logger = new Logger.Logger();
- entity.CallBackObject = i;
- logger.LogError(entity);
- Console.WriteLine(i);
- }
- }
- static void Main(string[] args)
- {
- LoggerTest test = new LoggerTest();
- test.Test();
- Console.ReadLine();
- }
- }
- public class OrmEntity:ILoggerEntity
- {
- public OrmEntity()
- {
- this.style = new OrmStyle("NND");
- }
- private string path;
- public string PathKey
- {
- get { return this.path; }
- set { this.path = value; }
- }
- /// <summary>
- /// 数据库连接字符串
- /// </summary>
- private string connectionString;
- public string ConnectionString
- {
- get { return this.connectionString; }
- set { this.connectionString = value; }
- }
- /// <summary>
- /// CommandType值之一
- /// </summary>
- private System.Data.CommandType cmdType;
- public System.Data.CommandType CmdType
- {
- get { return this.cmdType; }
- set { this.cmdType = value; }
- }
- /// <summary>
- /// DbParameter集合
- /// </summary>
- private System.Data.Common.DbParameterCollection paras;
- public System.Data.Common.DbParameterCollection Parameters
- {
- get { return this.paras; }
- set { this.paras = value; }
- }
- /// <summary>
- /// SQL语句
- /// </summary>
- private string cmdText;
- public string CmdText
- {
- get { return this.cmdText; }
- set { this.cmdText = value; }
- }
- /// <summary>
- /// 操作时间
- /// </summary>
- private DateTime controlTime;
- public DateTime ControlTime
- {
- get { return this.controlTime; }
- set { this.controlTime = value; }
- }
- /// <summary>
- /// 返回结果
- /// </summary>
- private object callBackObject;
- public object CallBackObject
- {
- get { return this.callBackObject; }
- set { this.callBackObject = value; }
- }
- /// <summary>
- /// 是否成功
- /// </summary>
- private bool isSuccess;
- public bool IsSuccess
- {
- get { return this.isSuccess; }
- set { this.isSuccess = value; }
- }
- #region ILoggerEntity 成员
- private ILoggerStyle style;
- /// <summary>
- /// 记录Logger的样式
- /// </summary>
- public ILoggerStyle Style
- {
- get
- {
- return this.style;
- }
- set
- {
- this.style = value;
- }
- }
- #endregion
- }
大家看到这里或许比较关系的应该上面的OrmStyle是怎么实现的,这样容易大家自定义自己的样式,下一篇我们将主要讨论样式和配置文件的使用方法。