以前记录日志一般都用自己写一个日志记录模块,相当的简单.复杂的程序就用entlib的logging模块,不过说实在的entlib的日志模块个人不是很喜欢,更能也不尽人意.不时会接触到log4.net,感觉相当的强大,但由于配置非常的复杂,一个项目可能整半天花在个log4上是很失败的.
做一个简单的封装,实现了常用的error日志和程序记录.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using log4net;
namespace Log
{
public static class Logger
{
private readonly static ILog logger = LogManager.GetLogger( "MyLogger" );
static Logger()
{
//读取配置信息
log4net.Config.DOMConfigurator.Configure( new FileInfo( "log4net.config" ) );
}
public static void WriteErrMsg( Exception ex, string strModule, string strFunc )
{
logger.Error( strModule + "." + strFunc, ex );
}
public static void WriteErrMsg( string Message, string strModule, string strFunc )
{
logger.Error( strModule + "." + strFunc + "/r/n" + Message );
}
public static void WriteLog( string strMsg )
{
logger.Info( strMsg );
}
......................................
}
}
在oracle 数据库中创建表:
create table log (
Datetime timestamp(3),
Thread varchar2(255),
Log_Level varchar2(255),
Logger varchar2(255),
Message varchar2(4000)
);
其他数据也创建类似的表
但是下面的config文件中红色字体的内容在下载的log4net项目中/doc/index.html中的config examples里面可以找到相应的。
把这个文件log4net.config放到程序的目录下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<root>
<!--
<level value="ALL" />
<appender-ref ref="rootFile" />
-->
</root>
<logger name="MyLogger">
<level value="ALL" />
<appender-ref ref="Loging" />
<appender-ref ref="ErrorLoging" />
<appender-ref ref="AdoNetAppender_Oracle">
</logger>
<appender name="Loging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}%newline%message%n" />
<param name="Header" value=" " />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="ErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Err.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}%newline%message%n" />
<param name="Header" value=" [Header] " />
<param name="Footer" value="[Footer] " />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[mydatabase];User ID=[user];Password=[password]" />
<commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<bufferSize value="128" />
<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>
<pParameterName 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="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
</log4net>
</configuration>
现在程序就会记录下两个日志文件,一个是错误异常的,一个是程序数据记录。并将log写入到数据库表中