log4net的只能当XmlConfigurator.Configure()被调用 [英] log4net only works when XmlConfigurator.Configure() is called

查看:5105
本文介绍了log4net的只能当XmlConfigurator.Configure()被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解, < A HREF =htt​​p://stackoverflow.com/questions/3519100/log4net-not-working>问题的一直 几个的,但不幸的是,我一直没能得到我的日志配置的工作。我曾在某个地方做一些很小的错误。

I understand that this question has been asked several times, but unfortunately, I haven't been able to get my logging configuration working. I have to be making some very small mistake somewhere.

我有一个.NET 4.5的MVC 4 / EF 5 Web应用程序,我试图让日志记录工作。该解决方案具有两个项目,一个用于DAO的和模型对象,和一个用于网站。
App.config文件看起来是这样的:

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:

<?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>

同样log4net的部分也被复制到Web.Config文件。

The same log4net section has also been copied into the Web.Config file.

我添加了下面这两个文件的AssemblyInfo.cs:

I added the following to both AssemblyInfo.cs files:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]

复制到输出目录设置为true两个Log.Config文件。

"Copy To Output Directory" is set to true for both Log.Config files.

这是我能似乎得到记录追加到输出文件的唯一方法第一记录语句运行之前调用XmlConfigurator.Configure()。我想我可以写一个门面做,当我第一次得到记录,但只是觉得不妥。

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?

How can I initialize the logger without calling the XmlConfigurator manually?

推荐答案

我曾经面临完全一样的问题。正如你正确地指出,你不应该需要显式调用 XmlConfigurator 时,包括该行的的AssemblyInfo.cs 。问题是当第一次使用log4net的是在不具有该线路上的assebmly。在我来说,我是用Topshelf.Log4Net的NuGet包,我的应用程序记录是通过第一个日志行。

I have faced exactly the same issue. As you rightly point out, you should not need to explicitly call XmlConfigurator when 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.

您可以只需登录一个行早在应用程序或者如果你不需要记录任何做什么我做了,并添加以下在应用程序的入口点

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));

这篇关于log4net的只能当XmlConfigurator.Configure()被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆