I have log4net implemented in a Visual Studio 2010 VB.NET Windows service. It used to work, but not anymore.
I have a sub RunTasks that gets started by the service every minute.
When I debug this procedure using a console project in the same solution, the logging works. However when I install the service and run it, and debug it using VS by attaching to the process, I found out that IsInfoEnabled, IsDebugEnabled, IsErrorEnabled etc. are all set to false, and I guess that’s why the logging is not working, although RunTasks is executed by the service.
I have the following class, if I understand things right the XMLConfigurator is started before GetLogger.
Imports System.Reflection
<Assembly: log4net.Config.XMLConfigurator(ConfigFile:="LogConfig.xml", Watch:=True)>
Public NotInheritable Class Logging
Private Shared _LogName As String
Private Shared _ilog As log4net.ILog
Private Sub New()
End Sub
Public Shared ReadOnly Property ILog() As log4net.ILog
Get
If _ilog Is Nothing Then
_ilog = log4net.LogManager.GetLogger(LogName)
End If
Return _ilog
End Get
End Property
Private Shared ReadOnly Property LogName() As String
Get
If _LogName = "" Then
'If GetType(MySettings).GetProperty("LogName") IsNot Nothing Then
' _LogName = GetType(MySettings).GetProperty("LogName").GetValue(MySettings.Default, Nothing)
'End If
If _LogName = "" Then _LogName = "DefaultLogger"
End If
Return _LogName
End Get
End Property
End Class
The logging itself looks like this:
ILog.Info("Starting RunTasks ... ")
And here is my LogConfig.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="textfileAppender" type="log4net.Appender.RollingFileAppender">
<file value="myLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} %-5level - %message%newline" />
</layout>
</appender>
<logger name="DefaultLogger">
<level value="ALL" />
<appender-ref ref="textfileAppender" />
</logger>
</log4net>
</configuration>
I found the answer myself, I had to move the following line of code:
… (see first code block above) to the AssemblyInfo.vb of the solutions startup project. XMLConfigurator has to be executed before GetLogger is.