AdoNetAppender在log4net不记录或抛出异常



我在我的log4net中定义了一个日志记录器。配置文件,应该使用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:WINDOWSMicrosoft.NetassemblyGAC_32System.Transactionsv4.0_4.0.0.0__b77a5c561934e089System.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:WINDOWSMicrosoft.NetassemblyGAC_32System.EnterpriseServicesv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.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:WINDOWSMicrosoft.NetassemblyGAC_32System.EnterpriseServicesv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.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 []

我还在其他地方定义了一个FileAppender,它工作得很好。

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

bufferSize value="1"

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

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

听起来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吗?验证数据库字段的大小和类型是否与配置中的参数匹配?

我刚刚遇到这个,这是因为配置没有初始化。所以这可能和OPs的问题不同。然而,在你的应用程序开始你不能忘记这一行:

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

log4net将愉快地继续,而不发出任何警告等。我最终下载了源代码并重新构建log4net,以便进行调试。供参考我的全球。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)]

相关内容

  • 没有找到相关文章

最新更新