log4net 配置速成(保存到数据库)
1. Properties/AssemblyInfo.cs 里加上2句代码:
a. using log4net.Config;
b. [assembly: XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
2. 把 Log4Net.config 文件 放在与web.config 一个目录。就是根目录咯。
3. 建数据库及LOG表。字段见 Log4Net.config 里的 AdoNetAppender 。
4. 修改 Log4Net.config 里的连接字符串connectionString 及 commandText 看是否插入的表名与建的表是否一致。
5. 使用咯:
代码
ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
logger.Debug("用Log4Net写入数据库日志");
logger.Error("这是一个错误日志");
logger.Fatal("这是一个致命的错误日志");
logger.Warn("这是一个警告日志");
6. 测试时 Log4Net.config里的 bufferSize 为1,在运行时记得修改一下。
附 Log4Net.config 文件:
代码
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=127.0.0.1;Initial Catalog=Data;Persist Security Info=True;User ID=sa;Password=password" />
<commandText value="INSERT INTO Log ([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>
<!-- Set this to "ERROR" before release -->
<!--Highest: OFF->FATAL->RROR->WARN->INFO->DEBUG->ALL :Lowest -->
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
<!-- <appender-ref ref="SmtpAppender" /> -->
<!--<appender-ref ref="FileAppender" /> -->
</root>
</log4net>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=127.0.0.1;Initial Catalog=Data;Persist Security Info=True;User ID=sa;Password=password" />
<commandText value="INSERT INTO Log ([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>
<!-- Set this to "ERROR" before release -->
<!--Highest: OFF->FATAL->RROR->WARN->INFO->DEBUG->ALL :Lowest -->
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
<!-- <appender-ref ref="SmtpAppender" /> -->
<!--<appender-ref ref="FileAppender" /> -->
</root>
</log4net>