简体   繁体   中英

Windows Service running application using Log4Net

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.

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.

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM