简单封装的Log4net
1. [代码]使用
log = new Logger(this.GetType());
log.Info("Hello world.");
2. [代码]第二个版本,修复了Console重复发送消息的问题
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;
using log4net.Appender;
using log4net.Layout;
using log4net.Core;
using log4net.Repository.Hierarchy;
namespace ShotInfoImport
{
class AutoLog
{
private Logger logger;
public AutoLog(Type this_getType)
{
//config the log4net to output the file
ILayout layout = new PatternLayout("%d[Thread:%t]%-5p %c [Line:%L] - %m%n");
IAppender fileAppender = new FileAppender(layout, "log.txt");
IAppender consoleAppender = new ConsoleAppender(layout);
BasicConfigurator.Configure(fileAppender);
//error it print the number is up !
BasicConfigurator.Configure(consoleAppender);
//init the log object to output
ILog iLog = LogManager.GetLogger(this_getType);
logger = (Logger)iLog.Logger;
logger.AddAppender(consoleAppender);
logger.AddAppender(fileAppender);
logger.Additivity = false;
}
public void Info(Object message)
{
logger.Log(Level.Info,message, null);
}
public void Info(Object message, Exception exception)
{?http://www.bizhizu.cn/linglei/?
logger.Log(Level.Info, message, exception);
}另类图片
public void Debug(Object message)
{
logger.Log(Level.Debug, message, null);
}
public void Debug(Object message, Exception exception)
{
logger.Log(Level.Debug, message, exception);
}
public void Warning(Object message)
{
logger.Log(Level.Warn, message, null);
}
public void Warning(Object message, Exception exception)
{
logger.Log(Level.Warn, message, exception);
}
public void Error(Object message)
{
logger.Log(Level.Error, message, null);
}
public void Error(Object message, Exception exception)
{
logger.Log(Level.Error, message, exception);
}
public void Fatal(Object message)
{
logger.Log(Level.Fatal, message, null);
}
public void Fatal(Object message, Exception exception)
{
logger.Log(Level.Fatal, message, exception);
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。