我通过 log4net 登录到数据库时遇到问题。
- 用户有权插入数据库
- 相同的插入语句在 Management Studio 中确实有效
- log4net 确实记录到文件但不记录到数据库
- 连接字符串正确(在其他应用程序中相同)
得到以下配置:
<log4net debug="true">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="Logs\JallaJalla (%date{yyyyMMdd HHmmss}).log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger %-5level - %message%newline" />
</layout>
</appender>
<appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
<threshold value="DEBUG"/>
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;" />
<commandText value="INSERT INTO [log].[tLogTable] ([Message],[Level]) VALUES (@message, @log_level)" />
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
</appender>
<logger name="ADONetAppender">
<level value="ALL" />
<appender-ref ref="ADONetAppender" />
</logger>
<logger name="FileLogger">
<level value="ALL" />
<appender-ref ref="FileAppender" />
</logger>
</log4net>
数据库中的两列是:
- 消息 varchar(max) 不为空
- 级别 varchar(50) 不为空
谢谢!