<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!--配置log4net解析--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <connectionStrings> <!--配置log4net写入Sqlite数据库的连接字符串--> <add name="sqlite" connectionString="Data Source=|DataDirectory|log4net.db;Version=3;Compress=True;UTF8Encoding=True;" /> </connectionStrings> <log4net> <!--配置输出到跟踪中--> <appender name="trace" type="log4net.Appender.TraceAppender, log4net"> <!--配置日志输出格式--> <layout type="log4net.Layout.PatternLayout, log4net" value="[%d][%t][%p][%c]%m%n%exception" /> </appender> <!--配置输出到文件中--> <appender name="file" type="log4net.Appender.RollingFileAppender, log4net"> <!--配置日志文件--> <file value="logs/log4net.txt" /> <!--配置日志文件存时,继续添加日志--> <appendToFile value="true" /> <!--配置日志文件名以时间格式创建--> <rollingStyle value="Date" /> <!--配置日志文件名的时间格式--> <datePattern value="yyyyMMddHHmmss" /> <!--配置日志输出格式--> <layout type="log4net.Layout.PatternLayout, log4net" value="[%d][%t][%p][%c]%m%n%exception" /> </appender> <!--配置输出到控制台中--> <appender name="console" type="log4net.Appender.ColoredConsoleAppender, log4net"> <!--配置日志输出格式--> <layout type="log4net.Layout.PatternLayout, log4net" value="[%d][%t][%p][%c]%m%n%exception" /> </appender> <!--配置输出到数据库中--> <appender name="database" type="log4net.Appender.AdoNetAppender, log4net"> <!--日志缓存,当日志数达到设置数时执行写入数据库--> <bufferSize value="1" /> <!--配置数据库连接字符串--> <connectionStringName value="sqlite" /> <!--配置数据库连接对象类型--> <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite" /> <!--配置数据库insert语句--> <commandText value="insert into T_Log4Net (C_Date, C_Thread, C_Level, C_Logger, C_Message) values (@date, @thread, @level, @logger, @message)" /> <!--配置数据库insert语句参数--> <parameter> <parameterName value="@date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout, log4net" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout, log4net" value="%t" /> </parameter> <parameter> <parameterName value="@level" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout, log4net" value="%p" /> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout, log4net" value="%c" /> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout, log4net" value="%m" /> </parameter> </appender> <root> <!--输出所有日志级别--> <level value="ALL" /> <!--输出跟踪日志--> <appender-ref ref="trace" /> <!--输出文件日志--> <appender-ref ref="file" /> <!--输出控制台日志--> <appender-ref ref="console" /> <!--输出数据库日志--> <appender-ref ref="database" /> </root> </log4net> </configuration>