2

I have defined a logger in my log4net.config file that is supposed to use the AdoNetAppender to log to an instance of SQL Server 2005. The logger is being called in my code, but no messages are being logged and no exceptions are being thrown.

Here is the part of my config file defining the logger and the 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>

I also enabled log4net's internal debugging, and it wasn't of much help either:

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, I have also defined a FileAppender elsewhere and it works fine.

4

3 回答 3

9

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

bufferSize value="1"

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

<root>
   <level value="INFO"/>
   <appender-ref ref="SummarySqlAppender"/>
</root>
于 2011-08-19T08:45:00.490 回答
0

我刚刚遇到这个,这是因为配置没有初始化。因此,这可能与 OP 问题不同。但是,在您的应用程序启动中,您不能忘记这一行:

[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)]
于 2014-03-19T01:30:34.557 回答
0

听起来 log4net 正在初始化,但是这个特定的附加程序或记录器有问题。尝试将文件附加程序添加到 Log4NetSummarySqlLogger 并确保实际使用记录器。

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

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

于 2011-08-23T15:59:07.333 回答