views:

58

answers:

3

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 servvice 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?

A: 

What windows account is the service running under? Does the account have permission to write to the eventlog?

geoff
This is not an answer, so you should have posted it as a comment to the question.
Oded
@Oded - While I agree that this could be in the comments, it also works as an answer. If @Imkk checks the permissions and finds that that is the problem, then this serves its purpose as an answer.
geoff
Ifs and perhaps. Add a comment, if that solved the problem, post it as an answer.
Oded
The service is configured to local system, and my user account is admin. Still not working
lmkk
A: 

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?

JohnC
EventLogAppender will not throw exceptions, for log4net has an own error handler. By default this error handler writes the exception to Console.Error.WriteLine and Trace.WriteLine on the first occurence. All further occurences are ignored. You may use a own error handler like at the bottom of this article: http://blogs.myfirstsharepoint.de/archive/2009/06/02/aus-der-praxis-%E2%80%93-securityexception-beim-loggen-mit-eventlogappender-von-log4net.aspx (article is in german, but that doesn't harm the code, right?)
Hinek
A: 

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

Hinek
My user account has admin rights, and no still no result.I have not been able to figure out what you excactly mean by defining a explicit source in my config? could you post a link or an example?
lmkk
And I didn´t get anything from the links you posted
lmkk
Sorry, sometimes I write a bit complicated. You already have configured an explicit Source: <applicationName value="Lantic YCS WebServer" /> ... I edited my question ...
Hinek