2
votes

For a project I have a WCF Service library (very simple at the moment) which is hosted in IIS 7.5 via a WCF Service Website project.

For that WCF Service library I need log4net to log some major events.

But after starting and accessing the website, no logfile is created.

Here are my configuration details:

WCF Service library:

App.config (is containing the following)

<appSettings>
    <add key="log4net-config-file" value="log4net.config"/>
    <!-- <add key="log4net.Internal.Debug" value="true"/> -->
</appSettings>

log4net.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\Temp\\Logfile.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="100KB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c - %m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>

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

On top of the Service class
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

In the Static Constructor (mainly due to fiddling around)
string configFile = ConfigurationManager.AppSettings["log4net-config-file"];
XmlConfigurator.Configure(new FileInfo(configFile));
logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

In the non-static constructor:
logger.Info("Hello world!");

Wcf Service Website project:

 Web.Config is basically the same as App.Config of service library

 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

What I have tried so far:

There is no output in DebugView and nothing about log4net in the IIS configured logs.

How can I find out what is wrong?

What can I do to find out why it is not working?

2

2 Answers

6
votes

Are you telling log4net where to find the configuration settings before logging? Something I have become a fan of doing is to create a small Console program to prove out my configuration works with an exe that I can drop anywhere quickly. Once that's working then I'll migrate the code over to the actual program. The benefit here is you can bypass IIS or other obstacles to prove out you've got the right code in place, then if it doesn't work you can troubleshoot things outside of the logging configuration as the culprit.

This site goes through a pretty simple explanation of what is entailed with using an app.config to configure log4net. Configure Log4net with app.config

You might also be able to use this in your AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

More information can be found here: Have log4net use application config file for configuration data

5
votes

The problem was:

When hosted in IIS, the log4net config file was searched in the current directory of IIS, which is in my case:

C:\Windows\System32\inetsrv

But the config file is saved in the physical path of the WCF service.

So I had in the Web.config

<appSettings>
    <add key="log4net_config" value="log4net.config" />
</appSettings>

And in the Constructor of the Service-class I call

var fullPath = ConfigurationManager.AppSettings["log4net_config"];

var physicalPath = System.Web.Hosting.HostingEnvironment.MapPath("/");

// to keep it compatible with self hosting 
if (physicalPath != null)
{
    fullPath = Path.Combine(physicalPath, fullPath);
}

var fileInfo = new FileInfo(fullPath);

XmlConfigurator.ConfigureAndWatch(fileInfo);