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

发布于 2024-11-17 11:50:36 字数 9411 浏览 6 评论 0原文

我已经使用 log4net 一段时间了,所以我对它并不陌生。但这是我在 Windows Server 2008 R2 64 位计算机上部署的第一个大型 .NET 4.0 解决方案。

当然,我首先搜索并找到了类似的主题。有一个话题听起来很相似—— 运行以发布模式构建的 .Net 4.0 Windows 应用程序时,log4net 不会记录 - 但我的问题仍然不同。

我的解决方案的一部分是利用 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]

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

很奇怪,因为它仍然可以在我的本地计算机上的发布版本中工作。

为了完整起见,这里是 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 []

I've been using log4net for a while so I'm not quite new to it. But this was my first larger .NET 4.0 solution deployed on a Windows Server 2008 R2 64-bit machine.

Of course I searched first and found similar topics. One topic sounds quite the same -
log4net doesn't log when running a .Net 4.0 Windows application built in Release mode
- But my issue is still different.

Part of my solution is a console application that utilizes log4net. Everything works fine until I switch to Release build. During test on my PC it still works fine. But on server side log4net won't work anymore.

So I enabled log4net internal debugging. This points to a repository conflict. I've never dived so deep into log4net to fully understand it. But the problem seems to be that it first creates a repository for the domain assembly (CRMFacade.Domain) and then tries to create one for the console application (CRMFacade.DataImport). This fails because a default repository already exists:

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]

The funny thing is: In Debug build the console application (CRMFacade.DataImport) is the first and only assembly that log4net creates a repository for. In either case CRMFacade.Domain is in Release build. CRMFacade.Domain also references log4net from the GAC in order to emit log output. But it doesn't have a log4net configuration. This is always provided by the assemblies using the domain assembly.

Weird because it still works in Release build on my local computer.

For the sake of completeness here's the log4net debug output where everything works just fine:

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 []

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-11-24 11:50:36

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

We experienced the same issues. It's still not clear to me what the root cause is, but we got log4net working again. We experienced the problems only in websites where we configure log4net using the XmlConfiguratorAttribute on the website assembly. We managed to work around this by calling XmlConfigurator.Configure() directly from Application_Start(). Not a pretty solution, but it seems to work.

吃素的狼 2024-11-24 11:50:36

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

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

发布模式下,NET_4_0缺失

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

If you download the latest log4net code, look on the Build tab for the log4net project in Visual Studio 2010.

In Debug mode the compiler symbols are NET;NET_2_0;NET_4_0.

In Release mode NET_4_0 is missing.

Change the symbols in Release mode to NET;NET_2_0;NET_4_0 before you build a Release version and you should have no problem. Look at the file properties of the finished DLL in Windows Explorer. Under "Details" and you will see the File Description is "Apache log4net for .NET Framework 4.0"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文