惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 老姜

SQL Server 2008 收缩日志 【原创】自定义Membership,KSPMembership发布,支持多数据库(mysql/oracle/sqlserver) 日志与工厂模式(二) The timeout period elapsed prior to obtaining a connection from the pool.数据库连接池满的原因 Asp.net mvc 入门介绍(公司内部讲座学习资料) 反射复习(Reflection) SERV-U 防火墙设置 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败 的解决方法 ASP.NET MVC 多语言解决方案 C#(ASP.NET)DateTime日期类型格式化显示 (转载) DLinq 仅支持SQL SERVER ,不支持Mysql/Oracle 从SQL SERVER 2000到SQL SERVER 2005 视图中存在 ORDER BY 语句的完全解决方案 一个中型OA系统的架构过程(二) Enterprise Library for .NET Framework 2.0 掌握 ASP.NET 之路:自定义实体类简介(转载-ORM、泛型等) 转载:一个中型OA系统的架构过程(一) WebServices层次划分 SQL SERVER 2000 存储过程不支持数组的解决方法! 通过日志来恢复到你要的时点
日志与工厂模式(一)
老姜 · 2009-06-12 · via 博客园 - 老姜

关键字:日志 微软企业库 Log4net 工厂模式

日志是一个成熟系统必不可少的组成部分,而大部分常用的日志无非就是记录时间与发生的消息,日志可以输出到文件,WINDOWS事件以及控制台等等,先来看下面一个简单的日志组件。

首先定义一个 ISimpleLog 接口

public interface ISimpleLog {
    void AddLog(string text);
    void AddLog(string text, DateTime time);
}

接口包含一个方法名称的两个重载,然后在此接口上写一个实现类,如下:

public class SimpleTxtFileLog : ISimpleLog {
        public void AddLog(string text) {
            AddLog(text, DateTime.Now);
        }
        public void AddLog(string text, DateTime time) {
            string msg = string.Format("时间:{0}   消息:{1}\r\n", time.ToString(), text);
            string path = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
            File.AppendAllText(path, msg);
        }
    }

SimpleTxtFileLog 类是将日志记录到文本文件中,文件的路径是当前运行程序的路径,文件名是Log.txt,每行输出格式形如:“时间:2009-6-11 18:18:34   消息:测试日志消息”。

再来实现一个控制台日志类 SimpleConsoleLog,将日志输出到控制台:

public class SimpleConsoleLog : ISimpleLog {
    public void AddLog(string text) {
        Console.WriteLine(text, DateTime.Now);
    }
    public void AddLog(string text, DateTime time) {
        string msg = string.Format("时间:{0}   消息:{1}\r\n", time.ToString(), text);
        Console.WriteLine(msg);
    }
}

再来实现一个SimpleEventLog类,将日志输出到WINDOWS事件当中。

public class SimpleEventLog : ISimpleLog {

    public void AddLog(string text) {
        AddLog(text, DateTime.Now);
    }

    public void AddLog(string text, DateTime time) {
        string Source = "SimpleEventLog";
        string Log = "Application";
        if (!EventLog.SourceExists(Source))
            EventLog.CreateEventSource(Source, Log);
        EventLog.WriteEntry(Source, text);
    }
}

客户端使用代码:

ISimpleLog log = new SimpleEventLog();
log.AddLog("test");
ISimpleLog log2 = new SimpleConsoleLog();
log2.AddLog("test");
ISimpleLog log3 = new SimpleTxtFileLog();
log3.AddLog("test");

当然,现实中一般你就会记录到一个日志的位置。如果将来需要记录到数据库中的话,在扩展实现ISimpleLog接口即可。

现在你会发现,客户端的代码中仍然在使用 new 进行实例化日志类,而这块可能是会变化的,能不能通过修改配置文件并且不用修改客户端代码就可以替换呢,当然可以,用如下的代码就能做到这点:

public class SimpleLogWrapper {
    public readonly string SimpleLogClassName = ConfigurationManager.AppSettings["SimpleLogClassName"];

    public void AddLog(string text) {
        ISimpleLog log = (ISimpleLog)Assembly.Load("IVSoft.Log.SimpleLog").CreateInstance(SimpleLogClassName);
        log.AddLog(text);
    }

    public void AddLog(string Msg, DateTime time) {
        ISimpleLog log = (ISimpleLog)Assembly.Load("IVSoft.Log.SimpleLog").CreateInstance(SimpleLogClassName);
        log.AddLog(Msg, time);
    }
}

需要在你的配置文件中增加一个配置:

<appSettings>
    <add key="SimpleLogClassName" value="IVSoft.Log.SimpleTxtFileLog" />
</appSettings>

这样,客户端就可以使用SimpleLogWrapper 来使用了。

以上代码都比较简单,实现了一个日志记录的简单方法,但是突然有一天,你发现了微软企业库中的日志组件,觉着这个挺好用的,想在系统中使用它,结果呢?你不得不修改大量的代码,造成了高度的耦合,怎么办?下一篇我们就会使用工厂模式来实现一个比较通用的日志组件,包括企业库中的日志和Log4net组件。