Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net not working in a windows service

I have a console app which I am converting into a windows service. As a console app my log4net logging is working fine. But converting it into a windows service, my log4net logging has stopped working.

I have added this to my assemblyInfo.cs in the service project:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] 

This is my service class with onstart and onstop:

private static log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private Builder _builder;

        public YCSWebServerService()
        {
            InitializeComponent();
            _builder = new Builder();
        }

        protected override void OnStart(string[] args)
        {
            _log.Info("YCSWebServerService started");
            _builder.Start();
        }

        protected override void OnStop()
        {
            _log.Info("YCSWebServerService stopped");
            _builder.Stop();
        }

I have a "specific" log4net config file added to my service project:

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

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </root>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <threshold value="DEBUG" />
      <applicationName value="Lantic YCS WebServer" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="(%-5level %date{hh}:%date{mm}:%date{ss} [%thread] %logger [%property{NDC}] ) %message %n" />
      </layout>
    </appender>
  </log4net>

</configuration>

Any ideas or tips?

like image 351
lmkk Avatar asked Jun 29 '10 09:06

lmkk


People also ask

Is log4net compatible with .NET core?

yes. Log4Net targets . NET Standard, which gives it a level of compatibility across the entirety of . NET core, including .


3 Answers

Did you just add a log4net section to your app.config file? In your question you mentioned that you have "specific log4net config file", but the sample you gave looks like the whole contents of app.config. If app.config is the case then you could have simpler string in AssemblyInfo.cs:

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

Also adding requirePermission="false" to the section in my app.config helped me when I fixed similar problem with log4net not logging to file for Windows Service (see extra attribute - requirePermission="false"):

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
  </configSections>
like image 196
demp Avatar answered Oct 04 '22 14:10

demp


Some things to try:

Does the user running the service have write permissions?

Are any exceptions getting logged to the windows logs?

Have you tried running the service in debug mode to see if Log4net is throwing any exceptions?

like image 37
JohnC Avatar answered Oct 04 '22 13:10

JohnC


https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293617

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/5bef59bc-a28f-4e6d-8ddb-730e12764162

In Windows Vista, Windows XP SP 2, Windows Server 2003, 2008 and Windows 7 a user needs Administrator rights to access the Security Log. Now when log4net tries to create a Event Log Source, all Logs are checked if it already exists. So with a Windows Service, the user of the Windows Server would need Administrator rights (which is not good).

Solution: configure a explicit Source in the log4net configuration (as you did: <applicationName value="Lantic YCS WebServer" />, Lantic YCS WebServer is your Source) and create this Source in you Setup (because Setup-User should have Administrator rights).

like image 29
Hinek Avatar answered Oct 04 '22 14:10

Hinek