引言

  log4net库是Apache log4j框架在Micorsoft.NET平台的实现,是一个帮组程序员将日志信息输出到各种目标(控制台、文件、数据库等)的工具。(百度百科)

  实际项目中使用log4net极大的方便程序猿记录系统运行过程中的日志信息,特别是对bs系统说是一个比较实用的工具。本文简单解释它的使用过程,都是最基本的最简单的运用,没有其他多余的解释只是简单使用。

使用步骤:

步骤一:

  在项目文件中创建一个类库,本例使用类库名:Log4NetUtility。引用log4net库的dll,添加配置文件log4net.cfg.xml,注意将该文件的属性设置为始终复制。配置文件都是一些固定的格式,可以做一些简单的改动。基本内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections> <!--日志记录组建配置-->
<log4net debug="true">
<!-- 调试日志配置 -->
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%thread][%-5level][%c] - %message%newline" />
</layout>
</appender> <!-- 文件日志配置 -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false"/>
<maxSizeRollBackups value="-1" />
<datePattern value="yyyy-MM-dd_HH'.log'" />
<lockingModel value="log4net.Appender.FileAppender.MinimalLock" />
<file value="Log\\" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%thread][%-5level]%message%newline" />
</layout>
</appender> <!-- Setup the root category, add the appenders and set the default priority -->
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
</root> <logger name="FileLogLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
</configuration>

步骤二:

  编写日志方法,如下:

public class Log4NetUtility
{
private static ILog fileLogger = LogManager.GetLogger("FileLogLogger");
private static ILog debugLogger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static Log4NetUtility()
{
string binPath = System.AppDomain.CurrentDomain.BaseDirectory;
string webFfileName = binPath + @"bin\Config\log4net.cfg.xml";
string appFileName = binPath + @"Config\log4net.cfg.xml";
if (File.Exists(webFfileName))
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(webFfileName));
else if (File.Exists(appFileName))
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(appFileName));
else
Console.WriteLine("找不到Log4net配置文件");
}
public static void DebugLog(String message)
{
debugLogger.Debug(message);
}
public static void Debug(object o, string message, Exception exception)
{
Type type = o.GetType();
fileLogger.Debug(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception);
}
public static void Debug(object o, String message)
{
Type type = o.GetType();
fileLogger.Debug(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message));
}
public static void Debug(String typeName, String message, Exception exception)
{
fileLogger.Debug(string.Format("[{0}] - {1}", typeName, message), exception);
}
public static void Debug(String typeName, String message)
{
fileLogger.Debug(string.Format("[{0}] - {1}", typeName, message));
}
public static void Info(object o, String message, Exception exception)
{
Type type = o.GetType();
fileLogger.Info(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception);
}
public static void Info(object o, String message)
{
Type type = o.GetType();
fileLogger.Info(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message));
}
public static void Info(string typeName, String message, Exception exception)
{
fileLogger.Info(string.Format("[{0}] - {1}", typeName, message), exception);
}
public static void Info(string typeName, String message)
{
fileLogger.Info(string.Format("[{0}] - {1}", typeName, message));
}
public static void Warn(object o, String message, Exception exception)
{
Type type = o.GetType();
fileLogger.Warn(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception);
}
public static void Warn(object o, String message)
{
Type type = o.GetType();
fileLogger.Warn(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message));
}
public static void Warn(string typeName, String message, Exception exception)
{
fileLogger.Warn(string.Format("[{0}] - {1}", typeName, message), exception);
}
public static void Warn(string typeName, String message)
{
fileLogger.Warn(string.Format("[{0}] - {1}", typeName, message));
}
public static void Error(object o, String message, Exception exception)
{
Type type = o.GetType();
fileLogger.Error(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception);
}
public static void Error(object o, String message)
{
Type type = o.GetType();
fileLogger.Error(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message));
}
public static void Error(string typeName, String message, Exception exception)
{
fileLogger.Error(string.Format("[{0}] - {1}", typeName, message), exception);
}
public static void Error(string typeName, String message)
{
fileLogger.Error(string.Format("[{0}] - {1}", typeName, message));
}
public static void Fatal(object o, String message, Exception exception)
{
Type type = o.GetType();
fileLogger.Fatal(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception);
}
public static void Fatal(object o, String message)
{
Type type = o.GetType();
fileLogger.Fatal(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message));
}
public static void Fatal(string typeName, String message, Exception exception)
{
fileLogger.Fatal(string.Format("[{0}] - {1}", typeName, message), exception);
}
public static void Fatal(string typeName, String message)
{
fileLogger.Fatal(string.Format("[{0}] - {1}", typeName, message));
}
}

步骤三:

  系统内进行调用:

Log4NetUtility.Log4NetUtility .Error(this, "log4net日志错误记录");

  至此,log4net的简单应用就介绍完了,本文还是那句话,就是介绍它的一些简单用法,告诉你如何使用它,具体其内部的原理还有更深层的东西还是交给以后吧。

最新文章

  1. iOS Developer:真机测试
  2. Java Abstract Class
  3. C语言中的atan和atan2(转)
  4. linux sort命令学习
  5. ueditor富文本编辑在 asp.net MVC下使用步骤
  6. kafka学习(一)-背景及架构设计
  7. XML 文档解析操作
  8. JavaScript事件收集
  9. Java程序单元测试工具对比——Parasoft Jtest与Junit
  10. FZU 2087 统计树边
  11. 谈谈事件对象-event
  12. iOS 使用AVAudioPlayer开发录音功能
  13. vscode打造最佳的markdown编辑器
  14. python第六天 函数 python标准库实例大全
  15. drf频率组件
  16. Zabbix Trigger表达式实例
  17. python中 staticmethod与classmethod区别
  18. aircrack-ng无线破解实验
  19. 20155310 2016-2017-2 《Java程序设计》第三周学习总结
  20. rocketmq 记

热门文章

  1. 【Java EE 学习 46】【Hibernate学习第三天】【多对多关系映射】
  2. Logging vs NoLogging
  3. sybase 收集常用sql语句
  4. AutoMapper搬运工之初探AutoMapper
  5. JAVA基础中的注意点
  6. python lambda
  7. WebForm控件Repeater
  8. C# 读本地INI文件方法
  9. iOS 消息推送(APNs) 傻瓜式教程
  10. Leetcode Construct Binary Tree from Inorder and Postorder Traversal