Question

I have a WCF service layer. It has 3 projects, Harness project (to debug the solution), Windows service Project and a class library with business logic(BL project).

I have configured log4net for the BL project as folows.

Assembly Info cs

// Configure log4net using the .config file 
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4netConfiguration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
  </appSettings>
  <log4net>   
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="C:\my_logs/my_service_logs/my_log_%date{ddMMyyyy}.log" />
      <appendToFile value="true" />      
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="30MB" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
        <!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />-->
      </layout>
    </appender>   
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</log4netConfiguration>

When I run the Harness project, logging works perfectly fine. Harness projects invokes the wcf service, which responds to incoming requests and logs without any issue.

But when I install the windows service, and when the BL is being invoked via the website logging does not work.

Logging is not configured for the harness project or the Windows Service project.

As I have been using log4net for different projects I am sure my configurations are correct.

But why its not logging when it runs through the Windows service? Is it some sort of a security issue? For the log file/folders I gave full control to all users. Still its not logging. Please help!

Was it helpful?

Solution

The log4net documentation for assembly attributes says this:

Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

So you need to move the assembly attributes into the Windows Service and add something like this to the Main method:

 LogManager.GetLogger("This call initialises the logging system from the current assembly attributes");

OTHER TIPS

It's hard pinpointing exactly what the problem is without more code but i know that services can be tricky regarding which folder they are running in. You should perhaps try to debug your service (use System.Diagnostics.Debugger.Launch() in your code to automagically attach to it) and check what is its current directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top