C# log4net 仅在调用 XmlConfigurator.Configure() 时有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16766236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4net only works when XmlConfigurator.Configure() is called
提问by Brad
I understand that thisquestionhasbeenaskedseveraltimes, but unfortunately, I haven't been able to get my logging configuration working. I have to be making some very small mistake somewhere.
我知道这个问题已经被问过几次了,但不幸的是,我一直无法让我的日志配置工作。我必须在某个地方犯一些非常小的错误。
I have a .NET 4.5 MVC 4/EF 5 web application and I'm trying to get logging to work. The solution has two projects, one for the DAO's and model objects, and one for the web site. The App.Config file looks like this:
我有一个 .NET 4.5 MVC 4/EF 5 Web 应用程序,我正在尝试让日志记录工作。该解决方案有两个项目,一个用于 DAO 和模型对象,另一个用于网站。App.Config 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="RebuildingTogetherEntities" connectionString="stuff..."/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<log4net configSource="Log.config" />
</configuration>
The same log4net section has also been copied into the Web.Config file.
相同的 log4net 部分也已复制到 Web.Config 文件中。
I added the following to both AssemblyInfo.cs files:
我在两个 AssemblyInfo.cs 文件中添加了以下内容:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]
"Copy To Output Directory" is set to true for both Log.Config files.
两个 Log.Config 文件的“复制到输出目录”都设置为 true。
The only way that I can seem to get logging to append to the output file is to call XmlConfigurator.Configure() before the first logging statement runs. I guess I can write a facade to do that when I first obtain the logger, but that just feels wrong.
我似乎可以将日志记录附加到输出文件的唯一方法是在第一个日志记录语句运行之前调用 XmlConfigurator.Configure()。我想我可以在第一次获得记录器时编写一个外观来做到这一点,但这感觉不对。
How can I initialize the logger without calling the XmlConfigurator manually?
如何在不手动调用 XmlConfigurator 的情况下初始化记录器?
回答by Peter
You do not need to call the XmlConfigurator manually if you use:
如果您使用,则不需要手动调用 XmlConfigurator:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]
However you have to add the tag to all your dlls(all assambly.cs files).
但是,您必须将标记添加到所有 dll(所有 assambly.cs 文件)中。
回答by Matt Cole
I have faced exactly the same issue. As you rightly point out, you should not need to explicitly call XmlConfiguratorwhen you include the line in your AssemblyInfo.cs. The problem comes when the first use of log4net is in an assebmly that doesn't have that line. In my case I was using the Topshelf.Log4Net NuGet package, and the first log line that my application logged was through that.
我遇到了完全相同的问题。正如您正确指出的那样,XmlConfigurator当您在AssemblyInfo.cs. 当第一次使用 log4net 是在没有该行的组件中时,问题就出现了。在我的例子中,我使用的是 Topshelf.Log4Net NuGet 包,我的应用程序记录的第一个日志行就是通过它。
You could just log a line early in your app or if you don't need to log anything do what I did and add the following at the entry point of the application
您可以在应用程序的早期记录一行,或者如果您不需要记录任何内容,请执行我所做的并在应用程序的入口点添加以下内容
LogManager.GetLogger(typeof(Program));

