Log4net在Debug中工作但在Release版本中失败

时间:2011-06-24 10:00:22

标签: c#-4.0 .net-4.0 log4net windows-server-2008-r2

我一直在使用log4net,所以我不是很新的。但这是我在Windows Server 2008 R2 64位计算机上部署的第一个更大的.NET 4.0解决方案。

当然我先搜索并找到类似的主题。一个主题听起来完全相同 - log4net doesn't log when running a .Net 4.0 Windows application built in Release mode - 但我的问题仍然不同。

我的部分解决方案是使用log4net的控制台应用程序。一切正常,直到我切换到发布版本。在我的电脑上测试期间它仍然正常。但是在服务器端,log4net将不再起作用。

所以我启用了log4net内部调试。这指向存储库冲突。我从来没有深入到log4net深入了解它。但问题似乎是它首先为域程序集(CRMFacade.Domain)创建一个存储库,然后尝试为控制台应用程序(CRMFacade.DataImport)创建一个存储库。此操作失败,因为已存在默认存储库:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [Global Assembly Cache]. (.NET Runtime [4.0.30319.225] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [CRMFacade.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Program Files (x86)\CRMFacade.DataImport\CRMFacade.Domain.dll]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [CRMFacade.Domain, 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: DefaultRepositorySelector: Creating repository for assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Program Files (x86)\CRMFacade.DataImport\CRMFacade.DataImport.exe]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]

有趣的是:在Debug构建中,控制台应用程序(CRMFacade.DataImport)是log4net为其创建存储库的第一个也是唯一的程序集。在任何一种情况下,CRMFacade.Domain都处于发布版本中。 CRMFacade.Domain还引用GAC中的log4net以发出日志输出。但它没有log4net配置。这始终由使用域程序集的程序集提供。

很奇怪,因为它仍适用于我本地计算机上的Release版本。

为了完整起见,这里是log4net调试输出,其中一切正常:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [Global Assembly Cache]. (.NET Runtime [4.0.30319.225] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Program Files (x86)\CRMFacade.DataImport\CRMFacade.DataImport.exe]
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [CRMFacade.DataImport, 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 [C:\Program Files (x86)\CRMFacade.DataImport\CRMFacade.DataImport.exe.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 [ConsoleAppender] type: [log4net.Appender.ConsoleAppender]
log4net: XmlHierarchyConfigurator: Setting Property [LevelMin] to Level value [INFO]
log4net: XmlHierarchyConfigurator: Setting Collection Property [AddFilter] to object [log4net.Filter.LevelRangeFilter]
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 [%timestamp [%thread] %-5level %type: %message%newline]
log4net: PatternParser: Converter [timestamp] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [level] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [type] Option [] 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: XmlHierarchyConfigurator: Created Appender [ConsoleAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [ConsoleAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Loading Appender [RollingFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [C:\\Logfiles\\APPS\\Corporate\\CRMFacade\\DataImport\\log.txt]
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 [2MB]
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 [%date [%thread] %-5level %logger - %message%newline]
log4net: PatternParser: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [level] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [logger] Option [] 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 [C:\Logfiles\APPS\Corporate\CRMFacade\DataImport]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: FileAppender: Opening file for writing [C:\Logfiles\APPS\Corporate\CRMFacade\DataImport\log.txt] append [True]
log4net: XmlHierarchyConfigurator: Created Appender [RollingFileAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [RollingFileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []

2 个答案:

答案 0 :(得分:7)

我们遇到了同样的问题。它仍然不清楚根本原因是什么,但我们让log4net再次运行。我们仅在使用网站程序集上的XmlConfiguratorAttribute配置log4net的网站中遇到了问题。我们设法通过直接从Application_Start()调用XmlConfigurator.Configure()来解决这个问题。不是一个漂亮的解决方案,但似乎有效。

答案 1 :(得分:3)

如果您下载最新的log4net代码,请在Visual Studio 2010中查看log4net项目的构建选项卡。

调试模式下,编译器符号为NET;NET_2_0;NET_4_0

发布模式NET_4_0 缺少

在构建发行版之前,将发布模式中的符号更改为NET;NET_2_0;NET_4_0,您应该没有问题。在Windows资源管理器中查看已完成的DLL的文件属性。在“详细信息”下,您将看到文件描述为“用于.NET Framework 4.0的Apache log4net