2014-01-20 38 views
0

我有一個VB.NET 3.5 Windows服務即將使用log4net,但是,我想知道是否有任何錯誤設置在我的代碼中,因爲我無法從log4net獲取任何日誌,log4net無法生成任何日誌文件

VB代碼

Imports System.Threading 
Imports log4net 
Imports log4net.Config 

Public Class Service1 
    Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(Service1)) 
    Dim cnt As Integer = 0 

    Protected Overrides Sub OnStart(ByVal args() As String) 
     ' Add code here to start your service. This method should set things 
     ' in motion so your service can do its work. 

     'log4net.Config.XmlConfigurator.Configure(My.Application.Info.DirectoryPath) 
     'BasicConfigurator.Configure() 
     XmlConfigurator.Configure(New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\log4netsetup.xml")) 
     System.IO.File.AppendAllText(My.Application.Info.DirectoryPath & "\www.txt", "WTF") 

     log.Debug("DEBUG: Service Started.") 
     log.Info("INFO: Service Started.") 

     Dim timer As New Timer(New TimerCallback(AddressOf downloadPrintJob), Nothing, 1000, 50000) 

    End Sub 

    Protected Overrides Sub OnStop() 
     ' Add code here to perform any tear-down necessary to stop your service. 
    End Sub 

    Private Sub downloadPrintJob(ByVal o As Object) 
     cnt += 1 
     System.IO.File.AppendAllText("c:\temp\print_service\aaa.txt", cnt) 
     log.Debug("this is the log message") 
    End Sub 
End Class 

log4netsetup.xml

<log4net> 
    <!-- A1 is set to be a ConsoleAppender --> 
    <appender name="A1" type="log4net.Appender.ConsoleAppender"> 

     <!-- A1 uses PatternLayout --> 
     <layout type="log4net.Layout.PatternLayout"> 
      <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" /> 
     </layout> 
    </appender> 

    <!-- Set root logger level to DEBUG and its only appender to A1 --> 
    <root> 
     <level value="DEBUG" /> 
     <appender-ref ref="A1" /> 
    </root> 
</log4net> 

的www.txt和aaa.txt可以同時產生,寫在他們裏面的文字,所以我認爲這不是一個問題相關 允許。請指教,謝謝。

更新:

感謝Astef,我提出XMLConfigurator到Service1.Designer.vb,但似乎不工作仍在。

' The main entry point for the process 
<MTAThread()> _ 
<System.Diagnostics.DebuggerNonUserCode()> _ 
Shared Sub Main() 
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase 

    ' More than one NT Service may run within the same process. To add 
    ' another service to this process, change the following line to 
    ' create a second service object. For example, 
    ' 
    ' ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1, New MySecondUserService} 
    ' 
    XmlConfigurator.Configure(New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\log4netsetup.xml")) 
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1} 

    System.ServiceProcess.ServiceBase.Run(ServicesToRun) 
End Sub 

回答

0

在任何記錄器被實例化之前,您必須配置log4net。

這裏是你的實例(在Service1靜態字段):

Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(Service1)) 

這裏是配置(Service1.OnStart法):

XmlConfigurator.Configure(New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\log4netsetup.xml")) 

我不是VB的傢伙,但我認爲,靜態字段初始值設定項在方法中的任何其他代碼之前運行,這就是問題所在。

嘗試在配置後立即在方法中實例化一個記錄器。