1.引入log4net.dll
2.建立log4net.config文件,文件内容如下
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--日志记录组建配置-->
<log4net>
<!-- Console部分log输出格式的设定 -->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- 日志文件部分log输出格式的设定 -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--日志文件名开头-->
<file value="Logs\Log_" />
<!--是否追加到文件-->
<appendToFile value="true" />
<!--混合使用日期和文件大小变换日志文件名-->
<rollingStyle value="Composite" />
<!--日期的格式-->
<datePattern value="yyyyMMdd'.txt'" />
<!--最大变换数量-->
<maxSizeRollBackups value="10"/>
<!--最大文件大小-->
<maximumFileSize value="500KB"/>
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 日志类:%logger property:[%property{NDC} 行:%4L] - 日志信息:%message%newline" />
</layout>
</appender>
<!--日志输出到ADO-->
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender,log4net">
<!--BufferSize为缓冲区大小-->
<bufferSize value="100" />
<!--引用-->
<connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<!--连接字符串-->
<connectionString value="data source=DbLogs\log.db" />
<!--插入语句-->
commandText value="insert into [Log2] ([Author],[LogDate],[Log_Level],[Message],[Location],[Exception]) Values(@Author,@Date,@Level,@Message,@Location,@Exception);" />
<commandType value="Text"/>
<!--操作者,暂时没用到-->
<parameter>
<parameterName value="@Author" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Operator}" />
</layout>
</parameter>
<!--记录时间-->
<parameter>
<parameterName value="@Date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<!--日志等级-->
<parameter>
<parameterName value="@Level" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<!--异常消息-->
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<!--异常位置-->
<parameter>
<parameterName value="@Location" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%location" />
</layout>
</parameter>
<!--错误-->
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%exception" />
</layout>
</parameter>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ADONetAppender"/>
</root>
</log4net>
</configuration>
3.添加到每个appender 后面,可以控制输出的级别
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
4.在AssemblyInfo.cs后面加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Config\log4net.config", Watch = true)]