当前位置: 代码迷 >> 综合 >> 反射(11)——练习:日志框架
  详细解决方案

反射(11)——练习:日志框架

热度:28   发布时间:2023-10-01 18:54:19.0

Logger.Log(“日志消息”);

定义ILog接口:

public interface ILog
{void Log(string msg);
}

读取C盘下c:\log.xml配置文件:

<config><logger type="Itcast.SqlLogger" path="c:\sqllogger.dll"></logger><logger type="Itcast.TextLogger" path="d:\test\textlogger.dll"></logger><logger type="Itcast.SMSLogger" path="d:\test\smslogger.dll"></logger>
</config>

可以通过asm.GetType(“Itcast.SqlLogger”)可以根据类全名获得类的Type,速度更快。

每一项logger为一个日志插件,type为插件类的全名,path为插件类所在的dll的路径。

Logger.Log方法依次加载c:\log.xml中定义的插件类,将日志信息记录到相应的存储中去。

调试代码:

Logger.Log("准备连接数据库");
Sqlconnection con......
try
{
..
}
cath()
{
Logger.Log("连接数据库失败");
}

 

  相关解决方案