Log4net does not work in windows service

I have a console application that I convert to a Windows service. As a console application, log4net is working fine. But turning it into a Windows service, the log4net log stops working.

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

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

This is my class of service 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 configuration 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?

+7
c # logging windows-services
source share
4 answers

Have you just added the log4net section to the app.config file? In your question, you mentioned that you have a “specific log4net configuration file”, but the sample you gave looks like all the contents of app.config. If this is an app.config application, you can have a simpler line in AssemblyInfo.cs:

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

Also adding requirePermission = "false" to the section of my .config application helped me when I fixed a similar problem with log4net without registering in the file for the Windows service (see the additional attribute - requirePermission = "false"):

 <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/> </configSections> 
+6
source share

Some things to try:

Does the user performing the service have write permissions?

Are there any exceptions that are logged in Windows logs?

Have you tried running the service in debug mode to find out if Log4net is excluding any exceptions?

+1
source share

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 requires Administrator rights to access the Security Log. Now that log4net tries to create the Source event log, all logs are checked if they already exist. Thus, using the Windows service, a Windows Server user will need administrator rights (which is bad).

Solution: configure the 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 the configuration (since the installer must have administrator rights).

+1
source share

Well, this question was asked a long time ago, I have a suggestion that can help someone who is looking for a solution to this problem. If you are converting a console application to a Windows service ... you must create an installer to install the application. When you install the application, it creates the installation folder for the service, usually in ProgramFile ..... (check this when you install your service .. it gives the path to the installation directory). If in the web.config file you specified the path to the log file as "Log \ Log.txt", then in the installation folder there will be a (Log) file (Log.txt). You waste your time looking for a log file in your application folder.

0
source share

All Articles