log4net中的AdoNetAppender没有记录或抛出异常

时间:2011-08-18 15:05:06

标签: log4net adonetappender

我在log4net.config文件中定义了一个记录器,该文件应该使用AdoNetAppender登录到SQL Server 2005的一个实例。我的代码中正在调用记录器,但没有记录任何消息,也没有异常被抛出。

以下是我的配置文件中定义记录器和appender的部分:

<logger name="Log4NetSummarySqlLogger">
  <level value="INFO"/>
  <appender-ref ref="SummarySqlAppender"/>
</logger>

<appender name="SummarySqlAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<!--<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[removed];initial catalog=[removed];integrated security=false;persist security info=True;User ID=[removed];Password=[removed]" />
<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>

我还启用了log4net的内部调试,它也没有多大帮助:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [<executable directory>\log4net.dll]. (.NET Runtime [4.0.30319.237] on Microsoft Windows NT 5.1.2600 Service Pack 3)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [WindowsService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [WindowsService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [<executable directory>\WindowsService.exe]
log4net: DefaultRepositorySelector: Assembly [WindowsService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [WindowsService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: XmlConfigurator: configuring repository [log4net-default-repository] using file [<executable directory>\log4net.config]
log4net: XmlConfigurator: configuring repository [log4net-default-repository] using stream
log4net: XmlConfigurator: loading XML configuration
log4net: XmlConfigurator: Configuring Repository [log4net-default-repository]
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [INFO].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="INFO",value=40000].
log4net: XmlHierarchyConfigurator: Loading Appender [FileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [<log directory>/info.log]
log4net: XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [100KB]
log4net: XmlHierarchyConfigurator: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [{%level}%date{MM/dd HH:mm:ss} - %message%newline]
log4net: PatternParser: Converter [literal] Option [{] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [}] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [date] Option [MM/dd HH:mm:ss] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: RollingFileAppender: Searched for existing files in [<log directory>]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: FileAppender: Opening file for writing [<log directory>\info.log] append [True]
log4net: XmlHierarchyConfigurator: Created Appender [FileAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [FileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Retrieving an instance of log4net.Repository.Logger for logger [Log4NetSummarySqlLogger].
log4net: XmlHierarchyConfigurator: Setting [Log4NetSummarySqlLogger] additivity to [True].
log4net: XmlHierarchyConfigurator: Logger [Log4NetSummarySqlLogger] Level string is [INFO].
log4net: XmlHierarchyConfigurator: Logger [Log4NetSummarySqlLogger] level set to [name="INFO",value=40000].
log4net: XmlHierarchyConfigurator: Loading Appender [SummarySqlAppender] type: [log4net.Appender.AdoNetAppender]
log4net: XmlHierarchyConfigurator: Setting Property [BufferSize] to Int32 value [100]
log4net: XmlHierarchyConfigurator: Setting Property [ConnectionType] to String value [System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]
log4net: XmlHierarchyConfigurator: Setting Property [ConnectionString] to String value [data source=<server>;initial catalog=<database>;integrated security=false;persist security info=True;User ID=<user>;Password=<password>]
log4net: XmlHierarchyConfigurator: Setting Property [CommandText] to String value [INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@log_date]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [DateTime]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.RawTimeStampLayout]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@thread]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [String]
log4net: XmlHierarchyConfigurator: Setting Property [Size] to Int32 value [255]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%thread]
log4net: PatternParser: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@log_level]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [String]
log4net: XmlHierarchyConfigurator: Setting Property [Size] to Int32 value [50]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%level]
log4net: PatternParser: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@logger]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [String]
log4net: XmlHierarchyConfigurator: Setting Property [Size] to Int32 value [255]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%logger]
log4net: PatternParser: Converter [logger] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@message]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [String]
log4net: XmlHierarchyConfigurator: Setting Property [Size] to Int32 value [4000]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%message]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: XmlHierarchyConfigurator: Setting Property [ParameterName] to String value [@exception]
log4net: XmlHierarchyConfigurator: Setting Property [DbType] to DbType value [String]
log4net: XmlHierarchyConfigurator: Setting Property [Size] to Int32 value [2000]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
'QTAgent32.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'QTAgent32.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'QTAgent32.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
log4net: XmlHierarchyConfigurator: Created Appender [SummarySqlAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [SummarySqlAppender] to logger [Log4NetSummarySqlLogger].
log4net: XmlHierarchyConfigurator: Retrieving an instance of log4net.Repository.Logger for logger [Log4NetEventLogger].
log4net: XmlHierarchyConfigurator: Setting [Log4NetEventLogger] additivity to [True].
log4net: XmlHierarchyConfigurator: Logger [Log4NetEventLogger] Level string is [INFO].
log4net: XmlHierarchyConfigurator: Logger [Log4NetEventLogger] level set to [name="INFO",value=40000].
log4net: XmlHierarchyConfigurator: Loading Appender [EventLogAppender] type: [log4net.Appender.EventLogAppender]
log4net: XmlHierarchyConfigurator: Setting Property [LogName] to String value [Application]
log4net: XmlHierarchyConfigurator: Setting Property [ApplicationName] to String value [ConcurFilesService]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [{%level}%date{MM/dd HH:mm:ss} - %message%newline]
log4net: PatternParser: Converter [literal] Option [{] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [}] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [date] Option [MM/dd HH:mm:ss] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: EventLogAppender: Source [ConcurFilesService] is registered to log []
log4net: XmlHierarchyConfigurator: Created Appender [EventLogAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [EventLogAppender] to logger [Log4NetEventLogger].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []

FWIW,我还在别处定义了一个FileAppender,它运行正常。

3 个答案:

答案 0 :(得分:9)

尝试将缓冲区大小设置为1.在刷新缓冲区之前,应用程序可能会退出,因此您看不到任何日志消息。

bufferSize value="1"

另一个可能的问题可能是记录器的名称。你真的有一个这个名字的记录器吗?尝试配置根记录器

<root>
   <level value="INFO"/>
   <appender-ref ref="SummarySqlAppender"/>
</root>

答案 1 :(得分:0)

听起来像log4net正在初始化好,但是这个特殊的appender或logger有些不对劲。尝试将文件追加器添加到Log4NetSummarySqlLogger并确保实际使用记录器。

如果生成的SQL无效,ADONetAppender也会失败。失败不会导致异常,但会发出一个ERROR日志条目写入STDERR。如果这是一个Windows GUI或服务应用程序,我强烈建议您使用控制台应用程序测试您的配置,以便更容易发现任何appender错误;这些错误日志如下所示:

log4net:ERROR [AdoNetAppender] Exception while writing to database
System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Da
te', table 'Test1.dbo.Log'; column does not allow nulls. INSERT fails.
The statement has been terminated.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolea
n breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj)...

我读到你已经测试了连接字符串;你有没有测试过appender生成的sql?验证数据库字段大小和类型是否与配置中的参数匹配?

答案 2 :(得分:0)

我刚碰到这个,这是因为配置没有初始化。所以这可能与OPs问题不同。但是,在您的应用开始时,您不能忘记这一行:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

log4net将很乐意继续,不会发出任何警告等。我最终下载源并重建log4net,以便我可以调试。作为参考,我的Global.asax看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]