Galbanum之Logger(一)

 一直在用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即写下日志.

dll文件下载地址: http://download.csdn.net/source/886850
源程序下载地址: http://download.csdn.net/source/886864
下面看一个使用例子:

  1. public class LoggerTest
  2.     {
  3.         private ILoggerStyle style;
  4.         public LoggerTest()
  5.         {
  6.             style = LoggerStyleFactory.GetLoggerStyle("orm");           
  7.         }
  8.         public void Test()
  9.         {
  10.             ThreadPool.QueueUserWorkItem(new WaitCallback(start1), 1);
  11.             ThreadPool.QueueUserWorkItem(new WaitCallback(start2), 2);            
  12.         }
  13.      
  14.         private void start1(object obj)
  15.         {
  16.             OrmEntity entity = new OrmEntity();
  17.             entity.PathKey = "path1";
  18.             entity.CmdText = "start0";
  19.             entity.CmdType = System.Data.CommandType.Text;
  20.             entity.ConnectionString = "LogInfo,在父目录";
  21.             entity.ControlTime = DateTime.Now;
  22.             entity.IsSuccess = true;
  23.             entity.Style = style;
  24.             for (int i = 20000; i < 40000; i++)
  25.             {
  26.                 ILogger logger = new Logger.Logger();
  27.                 entity.CallBackObject = i;
  28.                 logger.LogInfo(entity);
  29.                 Console.WriteLine(i);
  30.             }
  31.         }
  32.         private void start2(object obj)
  33.         {
  34.             OrmEntity entity = new OrmEntity();
  35.             entity.PathKey = "path2";
  36.             entity.CmdText = "start0";
  37.             entity.CmdType = System.Data.CommandType.Text;
  38.             entity.ConnectionString = "LogError,在子目录";
  39.             entity.ControlTime = DateTime.Now;
  40.             entity.IsSuccess = true;
  41.             entity.Style = style;
  42.             for (int i = 40000; i < 60000; i++)
  43.             {
  44.                 ILogger logger = new Logger.Logger();
  45.                 entity.CallBackObject = i;
  46.                 logger.LogError(entity);
  47.                 Console.WriteLine(i);
  48.             }
  49.         }      
  50.         static void Main(string[] args)
  51.         {
  52.             LoggerTest test = new LoggerTest();
  53.             test.Test();           
  54.             Console.ReadLine();
  55.         }
  56.     }
上例为Logger的使用方法,多线程的安全测试是4线程测试的,线程安全问题大家可以放心。下面OrmEntity的实体,
  1.  public class OrmEntity:ILoggerEntity
  2.     {
  3.         public OrmEntity()
  4.         {            
  5.             this.style = new OrmStyle("NND");
  6.         }
  7.         private string path;
  8.         public string PathKey
  9.         {
  10.             get { return this.path; }
  11.             set { this.path = value; }
  12.         }
  13.         /// <summary>
  14.         /// 数据库连接字符串
  15.         /// </summary>
  16.         private string connectionString;
  17.         public string ConnectionString
  18.         {
  19.             get { return this.connectionString; }
  20.             set { this.connectionString = value; }
  21.         }
  22.         /// <summary>
  23.         /// CommandType值之一
  24.         /// </summary>
  25.         private System.Data.CommandType cmdType;
  26.         public System.Data.CommandType CmdType
  27.         {
  28.             get { return this.cmdType; }
  29.             set { this.cmdType = value; }
  30.         }
  31.         /// <summary>
  32.         /// DbParameter集合
  33.         /// </summary>
  34.         private System.Data.Common.DbParameterCollection paras;
  35.         public System.Data.Common.DbParameterCollection Parameters
  36.         {
  37.             get { return this.paras; }
  38.             set { this.paras = value; }
  39.         }
  40.         /// <summary>
  41.         /// SQL语句
  42.         /// </summary>
  43.         private string cmdText;
  44.         public string CmdText
  45.         {
  46.             get { return this.cmdText; }
  47.             set { this.cmdText = value; }
  48.         }
  49.         /// <summary>
  50.         /// 操作时间
  51.         /// </summary>
  52.         private DateTime controlTime;
  53.         public DateTime ControlTime
  54.         {
  55.             get { return this.controlTime; }
  56.             set { this.controlTime = value; }
  57.         }
  58.         /// <summary>
  59.         /// 返回结果
  60.         /// </summary>
  61.         private object callBackObject;
  62.         public object CallBackObject
  63.         {
  64.             get { return this.callBackObject; }
  65.             set { this.callBackObject = value; }
  66.         }
  67.         /// <summary>
  68.         /// 是否成功
  69.         /// </summary>
  70.         private bool isSuccess;
  71.         public bool IsSuccess
  72.         {
  73.             get { return this.isSuccess; }
  74.             set { this.isSuccess = value; }
  75.         }
  76.         #region ILoggerEntity 成员
  77.         private ILoggerStyle style;
  78.         /// <summary>
  79.         /// 记录Logger的样式
  80.         /// </summary>
  81.         public ILoggerStyle Style
  82.         {
  83.             get
  84.             {
  85.                 return this.style;
  86.             }
  87.             set
  88.             {
  89.                 this.style = value;
  90.             }
  91.         }
  92.         #endregion
  93.     }

大家看到这里或许比较关系的应该上面的OrmStyle是怎么实现的,这样容易大家自定义自己的样式,下一篇我们将主要讨论样式和配置文件的使用方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值