Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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>

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

I added the following to both AssemblyInfo.cs files:

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

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...