0

In order to move data for a couple products, I have created a C# console application that uses Log4Net to track progress. Log4Net is configured in the console apps App.Config file and currently has two appenders, a ConsoleAppender and a RollingLogFileAppender.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="logs\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 %-5level %logger %method - %message%newline%exception" />
  </layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger %method - %message%newline%exception" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
  <appender-ref ref="ConsoleAppender" />
</root>

This application and its logging are working when run standalone. Now in order to automate the running of this console app, I am creating Windows Service to run the console app. The application does not require any user input so I did predict any issues when running it through the service. When the service runs it successfully calls the console application and I can see the results of its work, however, the logging does not seem to working. The logs at the location of the executable being called are not changing and I cannot find another instance of the log elsewhere on my machine. The service is running as LocalSystem so permissions should not be an issue. I have tried calling the application with the following settings:

ProcessStartInfo processStartInfo = new ProcessStartInfo(@"C:\Constellation\Dev\Caelum\Caelum\bin\Debug\Caelum.exe");
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;

I have tried both with and without the output redirects and neither worked. Any suggestions are greatly appreciated.

Evan Frisch
  • 1,334
  • 5
  • 22
  • 40
  • How have you setup the configuration? Is it in the App.config, or a separate file? – RB. Apr 26 '16 at 15:06
  • Does the account under which the service is run have write permissions to the location where the logs are stored? – wimh Apr 26 '16 at 15:07
  • @RB. The Log4Net configuration is in the App.Config file. – Evan Frisch Apr 26 '16 at 15:47
  • @Wimmel I am running the service as LocalSystem so it should have permissions to the folder. – Evan Frisch Apr 26 '16 at 15:48
  • Use SysInternals "Process Monitor" to see any accesses that are being made to a path ending "logs\log.txt" - this should quickly highlight (a) The location of the log file (if any), and (b) any permissions issues or similar, – RB. Apr 26 '16 at 15:56

2 Answers2

3

Your executable might not be executed in the exact same location as you placed it. I already had my application being executed somewhere in the system32 folder.

Try specifying an absolute path instead of a relative path to the log file to see if that fixes your problem.

See here for a similar issue.

Community
  • 1
  • 1
Chris Schmitz
  • 390
  • 4
  • 15
  • I haven't checked lately, but windows/system32 always used to be the default current folder for a service process, so yes, add the full path to the log file. – simon at rcl Apr 26 '16 at 15:51
  • I changed the log file path to the absolute path and I added the `Directory.SetCurrentDirectory`. Log4Net is now logging correctly. Just changing the path to the absolute path did not work. Thanks! – Evan Frisch Apr 26 '16 at 17:40
0

You may want to look at the following to verify your App.config is getting read by the Windows Service. https://stackoverflow.com/a/14074843/6256551

The relevant part of the answer is here:

"If you cannot find a corresponding .exe.config file, then it is possible that the code within the service is falling back to default values. In this case, you can place a properly named and formatted config file alongside the service executable and then restart the service and everything should be fine."

Community
  • 1
  • 1
James Fegan
  • 129
  • 1
  • 7