log4net 使用

 1. 代码中使用配置文件:

log4net.Config.DOMConfigurator.Configure(new FileInfo("log4netConfig.xml"));

2.配置文件样例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<appSettings>
</appSettings>
<log4net>

<!--当前使用的log方式-->
<root>

<!--文件形式记录日志 -->
<appender-ref ref="RollingFileAppender" />
<!--控制台控制显示日志-->
<appender-ref ref="ConsoleAppender" />
<!--Windows事件日志-->
<!--<appender-ref ref="EventLogAppender" />-->
<!--数据库事件日志-->
<!--<appender-ref ref="AdoNetAppender" />-->

</root>

<!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->

<!--定义输出到文件中-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file type="log4net.Util.PatternString" value="%property{AppName}\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false"/>
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<footer value="This log is written by UIH using log4net."/>
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO - info-->
<conversionPattern value="%date >> %-5level %message%newline" />
</layout>
<threshold value="ALL"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ALL" />
<levelMax value="FATAL" />
</filter>
</appender>

<!--定义输出到控制台命令行中-->
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date >> %-5level %message%newline" />
</layout>
<mapping>
<level value="FATAL" />
<foreColor value="Red" />
<backColor value="White" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Cyan" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<threshold value="ALL"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ALL" />
<levelMax value="FATAL" />
</filter>
</appender>

<!--定义输出到windows事件中-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date >> %-5level thread: %thread, %message%newline" />
</layout>
<threshold value="ERROR"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>

<!--定义输出到数据库中,这里举例输出到MySQL数据库中,数据库为:uihpacsdb-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<reconnectonerror value="True" />
<bufferSize value="10" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<connectionString value="data source=127.0.0.1;initial catalog=uihpacsdb;User ID=root;Password=uihpacs2012;persist security info=True;Connect Timeout=15;" />
<commandText value="INSERT INTO pacs_log (Datetime,ThreadID,LogLevel,Logger,Message,LogException) VALUES (?log_date, ?thread, ?log_level, ?logger, ?message, ?exception)" />

<!--分级输出-->
<!--定义各个参数-->
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>

<threshold value="WARN"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>

</appender>

</log4net>
</configuration>
     


3. 代码中打印log

 log4net.LogManager.GetLogger("PIX").Error(ex.Message);
posted @ 2013-07-04 09:28  muzizongheng  阅读(274)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?