Search code examples
c#windows-serviceslog4netlog4net-configuration

Log4net doesn't work in windows service


I'm very frustrated in making log4net in my windows service. I've tried searching on Google but no solution solves my problems.

Here is my App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />    
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <appSettings>
    <add key="ApplicationSettings" value="Configurations/ApplicationSettings.json" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>

  <!-- log4net rolling file configuration -->
  <log4net debug="true">
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" 
             type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" 
             type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

In program.cs at Main function, I tried calling:

System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
XmlConfigurator.Configure();

In OnStart function of Main.cs (my service), I tried:

_log = LogManager.GetLogger(typeof(Main));
_log.Error("Service started");

But nothing happens, log file doesn't appear when I write log.

Can anyone help me please?

I spent my whole day searching for solution but found nothing.

Thank you,


Solution

  • It looks like you have a config issue above:

    <appender name="RollingFileAppender"
    
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender" />
    

    The names above have to match. So both have to be set to RollingFileAppender for example.

    Also when it comes to writing log files I tend not to try and write my log files anywhere in the program files directory or anything in any virtual directory just because of the previous battles I've had with security issues. Currently I'm using something like the following for all my log4net log files:

    <file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/<Product Name>/Logs/<Program Name>/<Program Name>.log" />
    

    ${ALLUSERSPROFILE} is the key above. This directory typically doesn't have the security restrictions as virtual directory and the program files directory. I've found that I haven't had any trouble since I've been using this path.

    This envrionment variable takes you to the ProgramData directory in Windows Vista, 7, 8, Server 2008 etc. I think XP takes you to a different place but still a directory with relaxed permissions. All you have to do is type in %allusersprofile% in file explorer and it will take you there.