在软件系统中难免会用到日志记录的需求,接下来我将给大家详细介绍下Log4net写入到文件、sqlserver和oracle的方法。
首先,我们把Log4net.dll添加到引用中。
第二步,在config文件中编写log4net的相关配置,下面将详细介绍写入文件配置、写入sqlserver配置和写入oracle配置
1.文件记录
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
- </configSections>
- <appSettings>
- </appSettings>
- <log4net>
- <!--定义输出到文件中-->
- <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
- <!--定义文件存放位置-->
- <file value="LogInfo.txt" />
- <appendToFile value="true" />
- <layout type="log4net.Layout.PatternLayout">
- <!--每条日志末尾的文字说明-->
- <footer value="" />
- <!--输出格式-->
- <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
- <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
- </layout>
- </appender>
- <appender name="LogFileAppender_Size" type="log4net.Appender.RollingFileAppender">
- <file value="LogInfo.txt" />
- <appendToFile value="true" />
- <rollingStyle value="Size" />
- <maxSizeRollBackups value="10" />
- <maximumFileSize value="1000KB" />
- <staticLogFileName value="true" />
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
- </layout>
- </appender>
- <appender name="LogFileAppender_Date" type="log4net.Appender.RollingFileAppender">
- <file value="LogInfo.txt" />
- <appendToFile value="true" />
- <rollingStyle value="Date" />
- <datePattern value="yyyyMMdd" />
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
- </layout>
- </appender>
- <root>
- <!--日志记录方式-->
- <appender-ref ref="LogFileAppender_Size" />
- </root>
- </log4net>
- </configuration>
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
- </configSections>
- <appSettings>
- </appSettings>
- <log4net>
- <appender name="AdoNetAppender_SQLServer" type="log4net.Appender.AdoNetAppender">
- <!--设置缓存区大小
- 1表明有一条日志就要写入
- 如果10就表示日志到达10条时一起写入
- -->
- <bufferSize value="1" />
- <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
- <connectionString value="data source=192.168.0.108;initial catalog=RTData;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
- <commandText value="INSERT INTO LogInfo ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 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="4000" />
- <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>
- </appender>
- <!--定义日志的输出媒介-->
- <root>
- <appender-ref ref="AdoNetAppender_SQLServer" />
- </root>
- </log4net>
- </configuration>
还要在sqlserver数据库中建立一个跟配置文件中同表名、同表结构的日记表。
3.Oracle记录
- <?xml version="1.0"?>
- <configuration>
- <configSections>
- <section name="extnet" type="Ext.Net.GlobalConfig" requirePermission="false"/>
- <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
- </configSections>
- <appSettings>
- <add key="ChartImageHandler" value="storage=file;timeout=20;"/>
- </appSettings>
- <!--
- 用log4net记录日志信息到Oracle数据库中
- 要在数据库中新建表
- create table BASE_ERRORLOG
- (
- LOG_DATE DATE,
- MESSAGE VARCHAR2(4000)
- )
- -->
- <log4net>
- <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
- <connectionString value="Data Source=GS_DB;Persist Security Info=True;User ID=YPQ_SJ;Password=Sg58895168;Min Pool Size=5;Max Pool Size=50;"/>
- <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
- <commandText value="INSERT INTO Base_ErrorLog (LOG_DATE,MESSAGE) VALUES (:log_date,:message)"/>
- <bufferSize value="1"/>
- <!--定义各个参数-->
- <parameter>
- <parameterName value=":log_date"/>
- <dbType value="DateTime"/>
- <layout type="log4net.Layout.RawTimeStampLayout"/>
- </parameter>
- <parameter>
- <parameterName value=":message"/>
- <dbType value="String"/>
- <size value="4000"/>
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%message"/>
- </layout>
- </parameter>
- </appender>
- <root>
- <appender-ref ref="AdoNetAppender_Oracle" />
- </root>
- </log4net>
- </configuration>
还要在oracle数据库中建立一个跟配置文件中同表名、同表结构的日记表。
具体使用那种记录方式,自己选定。
第三步,新建一个Logger.cs类
- using System;
- using System.Reflection;
- using log4net;
- //注意下面的语句一定要加上,指定log4net使用.config文件来读取配置信息,如不指定配置文件路径,则按以下方式获取
- //如果是WebForm,则从web.config中读取相关信息
- //ConfigFile是你写log4net配置的地方
- [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
- namespace com.yitongyu.common.log
- {
- /// <summary>
- /// 日志记录器
- /// </summary>
- public static class Logger
- {
- private static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- /// <summary>
- /// 获取当前 Ilog 对象
- /// </summary>
- public static ILog GetILog
- {
- get
- {
- return log;
- }
- }
- /// <summary>
- /// 记录错误日志
- /// </summary>
- /// <param name="message"></param>
- /// <param name="ex"></param>
- public static void Error(object message, Exception ex)
- {
- log.Error(message, ex);
- }
- /// <summary>
- /// 记录严重错误
- /// </summary>
- /// <param name="message"></param>
- /// <param name="ex"></param>
- public static void Fatal(object message, Exception ex)
- {
- log.Fatal(message, ex);
- }
- /// <summary>
- /// 记录一般信息
- /// </summary>
- /// <param name="message"></param>
- public static void Info(object message)
- {
- log.Info(message);
- }
- /// <summary>
- /// 记录调试信息
- /// </summary>
- /// <param name="message"></param>
- public static void Debug(object message)
- {
- log.Debug(message);
- }
- /// <summary>
- /// 记录警告信息
- /// </summary>
- /// <param name="message"></param>
- public static void Warn(object message)
- {
- log.Warn(message);
- }
- }
- }
第四步,调用日志记录方法
- Logger.Erroe("错误信息",null);