用log4net 发送日志到数据,怎么没反应,记录日志文件可以。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<appSettings>
</appSettings>
<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=.;Initial Catalog=test;User ID=sa;Password=sa" />
<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>
</configuration>
log4net 写入SQL数据库
[解决办法]
提醒一下:
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 看是否插入的表名与建的表是否一致。
详见:http://myx.cnblogs.com/iaxes/articles/Log4net.html