log4net 配置应用

(一). WinForm 或者 WPF 中的配置和应用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandle,log4net"/>
</configSections> <log4net>
<root>
<level value="INFO " /> <!-- 日志等级 OFF > FATAL > ERROR > WARN > INFO > DEBUG  > ALL -->
<appender-ref ref="LogFileAppender" />
</root>
<!--<logger name="WebLogger">
<level value="WARN"/>
<appender-ref ref="LogFileAppender" />
</logger>--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="D:Log/"/>
<param name="AppendToFile" value="true" />
<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<!--<file value="./log.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="1MB" />
<staticLogFileName value="false" />
<DatePattern value="yyyy-MM-dd&quot;.txt&quot;"/>-->
<!--<datePattern value="yyyyMMdd-HH:mm:ss" />-->
<layout type="log4net.Layout.PatternLayout">
<!--每条日志开始的文字说明-->
<param name="Header" value="[Header]\r\n" />
<!--每条日志末尾的文字说明-->
<param name="Footer" value="[Footer]\r\n"/>
<!--输出格式-->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<!--<conversionPattern value="%date %-5level - %message%newline" />-->
</layout>
</appender>
</log4net>
</configuration>
       var path = AppDomain.CurrentDomain.BaseDirectory + @"\log4net_config.xml";//获取配置文件路径
//var path = AppDomain.CurrentDomain.BaseDirectory + @"\log4netConfig.xml";
log4net.Config.XmlConfigurator.Configure(new FileInfo(path)); log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
if (log.IsWarnEnabled)
log.Warn("message");

(二).MVC 4  应用

  <configSections>
<!--Lo4Net 配置-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandle,log4net"/>
</configSections>
<log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<root>
<level value="ERROR"/>
<appender-ref ref="SysAppender"/>
</root> <!-- Print only messages of level DEBUG or above in the packages -->
<!--<logger name="WebLogger">
<level value="DEBUG"/>
</logger>--> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<file value="App_Data/Log/" />    <!-- 一定注意路径 -->
<param name="AppendToFile" value="true" />
<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<!--<file value="./log.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="1MB" />
<staticLogFileName value="false" />
<DatePattern value="yyyy-MM-dd&quot;.txt&quot;"/>-->
<!--<datePattern value="yyyyMMdd-HH:mm:ss" />-->
<layout type="log4net.Layout.PatternLayout">
<!--每条日志开始的文字说明-->
<!--<param name="Header" value="[Header]\r\n" />-->
<!--每条日志末尾的文字说明-->
<!--<param name="Footer" value="[Footer]\r\n"/>-->
<!--输出格式-->
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
<appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
 public class MvcApplication : SpringMvcApplication
{
  protected void Application_Start()
{
//log4net 注册,使用路径
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));

        ILog logger = LogManager.GetLogger(this.GetType());
        logger.Error(e.ToString());//将异常信息写到磁盘上.

        }
}

(三)数据库写入日志配置

<!--数据库记录日志-->
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=192.168.1.188\MSSQLSERVER2008;database=HotelMIS;uid=sa;pwd=123456;" />
<commandText value="insert into Log(LogLevel, Msg, Exception, Cdt) values(@LogLevel, @Msg, @Exception, @Cdt)" />
<bufferSize value="1" />
<parameter>
<parameterName value="@LogLevel" />
<dbType value="String" />
<size value="128" />
<layout type="log4net.Layout.PatternLayout" >
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@Msg" />
<dbType value="String" />
<size value="1024" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<size value="10240" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter>
<parameterName value="@Cdt" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
</appender>

(四) 关于单独出来的Log帮助类的写法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; [assembly: log4net.Config.XmlConfigurator(Watch = false)]
namespace Util
{
public class Log
{
public static void Error(string msg)
{
//log4net 注册,使用路径
//log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("~/Web.config"));
ILog logger = LogManager.GetLogger("WebLogger");
logger.Error(msg);//将异常信息写到磁盘上.
} }
}
上一篇:Android逆向分析(2) APK的打包与安装


下一篇:python字符串截取与替换的例子