0
votes

I m implementing a windows service with log4net logger. The thing is when I debug the service, the log is created but after installing, the log is not creating. I used app.config file and another config file to add configuration details

This is my app.config file

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />   </startup>   <appSettings>
<!--Log file configuration-->
<add key="log4net.Config" value="log4.config"/>
<add key="log4net.Config.Watch" value="True"/>
<add key="log4net.Internal.Debug" value="false"/>   </appSettings> </configuration>

and this is another config file named log4.config

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>   
  <configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>

<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="D:\applications\ActualSys\FujiOrder\log\log.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFileAppender" />
</root>   </log4net> </configuration>

I am using following way to create log messages

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

log.Info("Interface " + CODE + " - sendOrderThreadRoutine started!");

This works perfectly when I m debugging. But this is not working after install the service.

I have tried following code snippets with AssemblyInfo.cs file. But the issue has not fixed

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

When I install the service, below files are in the installed folder

  1. .exe file
  2. .config file
  3. log4net.dll

Even though I can see lot of examples for log file is not creating in the debug phase, I couldn't find articles about the same issue in the installation phase.

1
Are you installing the service on the same machine as the one you're debugging on? Under what credentials is the service running? Could be a permissions problem.jeroenh
Why do you need another config file? Did you try placing them all into app.config?Orkun Bekar

1 Answers

0
votes

Start by enabling log4net debuging in your configuration:

<configuration>
   <appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
   </appSettings>
</configuration>

log4net faq

Before you start logging, you need to call log4net configure. Because when you running as a service, you have a different entry point that when you are running in the debugger. Check if you indeed call the configure.

An other problem can be that there is some kind of access denied when writing to the logging directory. You are running as a different user. This will be in the debug logs of log4net. Or the file can be locked by you debug session.