使用log4net记录日志到MySQL中 (转)
http://www.cnblogs.com/kongxx/archive/2005/10/09/251073.aspx
在log4net是.net平臺中日志記錄的開源工具包,其中包括了很多記錄日志的方式和模板,詳細可以參考log4net的document,這里僅僅就在使用log4net向MySQL記錄日志做說明。
1)首先需要到mysql的官方網站(www.mysql.com)下載驅動,網站上提供了兩個驅動,這里我使用的mysql-connector-net連接器,版本是1.0.6。其中在下載的時候會有兩種包,一種是.exe安裝包,一種是no-install包,我這里使用的是no-install包(文件名:mysql-connector-net-1.0.6-noinstall.zip)。解壓縮zip文件,復制bin\net-1.1\release\MySql.Data.dll到項目下;
2)在項目中添加引用,選擇MySql.Data.dll;
3)在本機的mysql上使用test數據庫,使用一下sql創建表:
??id?int(11)?NOT?NULL?auto_increment,
??log_datetime?timestamp?NOT?NULL,
??log_thread?varchar(255)?default?NULL,
??log_level?varchar(255)?default?NULL,
??log_logger?varchar(255)?default?NULL,
??log_message?varchar(255)?default?NULL,
??PRIMARY?KEY??(id)
)?
4)修改app.config添加以下內容
<configuration>
????<configSections>????????
????????<section?name="log4net"?type="log4net.Config.Log4NetConfigurationSectionHandler,?log4net"?/>????????
????</configSections>
????
????<log4net>
????????<appender?name="stdout"?type="log4net.Appender.ConsoleAppender">
????????????<layout?type="log4net.Layout.PatternLayout">
????????????????<param?name="ConversionPattern"?value="[%d?%l]?%m%n"?/>
????????????</layout>
????????</appender>
????????
????????<appender?name="ADONetAppender"?type="log4net.Appender.ADONetAppender">
????????????<param?name="ConnectionType"?value="MySql.Data.MySqlClient.MySqlConnection,?MySql.Data"?/>
????????????<param?name="ConnectionString"?value="database=test;server=localhost;user?id=root;password=;old?syntax=yes"/>
????????????<param?name="CommandText"?value="insert?into?mylog(log_datetime,log_thread,log_level,log_logger,log_message)?values(@log_datetime,?@log_thread?,?@log_level,?@log_logger,?@log_message)"?/>
????????????
????????????<param?name="Parameter">
????????????????<param?name="ParameterName"?value="@log_datetime"?/>
????????????????<param?name="DbType"?value="DateTime"?/>
????????????????<param?name="Layout"?type="log4net.Layout.PatternLayout">
????????????????????<param?name="ConversionPattern"?value="%d{yyyy'-'MM'-'dd?HH':'mm':'ss'.'fff}"?/>
????????????????</param>
????????????</param>
????????????<param?name="Parameter">
????????????????<param?name="ParameterName"?value="@log_thread"?/>
????????????????<param?name="DbType"?value="String"?/>
????????????????<param?name="Size"?value="255"?/>
????????????????<param?name="Layout"?type="log4net.Layout.PatternLayout">
????????????????????<param?name="ConversionPattern"?value="%t"?/>????????????????
????????????????</param>
????????????</param>
????????????<param?name="Parameter">
????????????????<param?name="ParameterName"?value="@log_level"?/>
????????????????<param?name="DbType"?value="String"?/>
????????????????<param?name="Size"?value="50"?/>
????????????????<param?name="Layout"?type="log4net.Layout.PatternLayout">
????????????????????<param?name="ConversionPattern"?value="%p"?/>
????????????????</param>
????????????</param>
????????????<param?name="Parameter">
????????????????<param?name="ParameterName"?value="@log_logger"?/>
????????????????<param?name="DbType"?value="String"?/>
????????????????<param?name="Size"?value="255"?/>
????????????????<param?name="Layout"?type="log4net.Layout.PatternLayout">
????????????????????<param?name="ConversionPattern"?value="%c"?/>
????????????????</param>
????????????</param>
????????????<param?name="Parameter">
????????????????<param?name="ParameterName"?value="@log_message"?/>
????????????????<param?name="DbType"?value="String"?/>
????????????????<param?name="Size"?value="255"?/>
????????????????<param?name="Layout"?type="log4net.Layout.PatternLayout">
????????????????????<param?name="ConversionPattern"?value="%m"?/>
????????????????</param>
????????????</param>
????????</appender>
????????<appender?name="file"?type="log4net.Appender.RollingFileAppender">
????????????<param?name="File"?value="log\\log"?/>
????????????<param?name="AppendToFile"?value="true"?/>
????????????<param?name="RollingStyle"?value="Date"?/>
????????????<param?name="DatePattern"?value="yyyy-MM-dd"?/>
????????????<layout?type="log4net.Layout.PatternLayout">
????????????????<param?name="ConversionPattern"?value="[%d?%l]?%m%n"?/>
????????????</layout>
????????</appender>
????????<root>
????????????<level?value="ALL"?/>
????????????<appender-ref?ref="ADONetAppender"?/>
????????</root>
????????<logger?name="*">
????????????<level?value="ALL"?/>
????????????<appender-ref?ref="ADONetAppender"?/>
????????</logger>
????</log4net>
</configuration>
5)在代碼中使用一下代碼測試
??? log.Info("==========================");
如果順利應該OK了,呵呵。
轉載于:https://www.cnblogs.com/jambol/archive/2007/08/23/866394.html
總結
以上是生活随笔為你收集整理的使用log4net记录日志到MySQL中 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 键盘钩子程序
- 下一篇: Quartz.net官方开发指南 第九课