将日志保存到数据库只web.config中的配置有一点改进,其它的都是相同的。
第1步,创建日志表
- create table tb_log (
- Datetime timestamp(3),
- Thread varchar2(255),
- Log_Level varchar2(255),
- Logger varchar2(255),
- Message varchar2(4000)
- );
第2步,配置web.config
- <configuration>
- <configSections>
- <section name="log4net"
- type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
- </configSections>
- <log4net>
- <root>
- <level value="ALL"/>
- <appender-ref ref="AdoNetAppenderOracle"/>
- </root>
- <appender name="AdoNetAppenderOracle" 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>
- <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>
- </appender>
- </log4net>
- …
- </configuration>
第3步,开始测试。
参考网站:
http://logging.apache.org/log4net/release/config-examples.html
本文转自 彭金华 51CTO博客,原文链接:http://blog.51cto.com/pengjh/584533