2

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?

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113

2 Answers2

6

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

Community
  • 1
  • 1
Thomas
  • 152
  • 1
  • 2
  • 8
  • Thank you very much for your answer! Yes, I already have a Selfhosting inplace for my service library where the logging works like a charm. And as can be seen in the question, i have the entry in the AssemblyInfo.cs. But still, no logfile gets created when hosting in IIS. – Mare Infinitus Aug 14 '14 at 22:05
  • Ah! Apparently I am blind to scroll bars. Sorry about that. So it seems like maybe you're looking at a permissions issue on the temp directory? Have you tried to write to a different directory other than temp? When looking up the IIS users that the service could be running under I came across this [Breakdown of IIS Users](http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they-differ) One of the comments in the question I linked says you should avoid using XmlConfigurator.Configure when the AssemblyInfo attribute it specified. – Thomas Aug 15 '14 at 14:11
  • Okay, so only assemby: config and no direct call to configure. Will try that! Will try another directory also! Thank you – Mare Infinitus Aug 15 '14 at 18:26
  • Hope you managed to get it figured out. Let me know if not and I'll see f I can come up with anything else to try – Thomas Aug 18 '14 at 18:02
  • Yes, it works now. The problem was that the config was not found. So I had to get the physical path and call `Configure` with that. Seems like I should provide an answer for that. Thank you for your help! – Mare Infinitus Aug 18 '14 at 19:03
  • Yeah I would definitely. Good job figuring that out! – Thomas Aug 18 '14 at 20:57
5

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);
Örjan Jämte
  • 14,147
  • 1
  • 23
  • 23
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113