log4net无法生成任何日志文件

时间:2014-01-20 10:16:05

标签: .net

我有一个VB.NET 3.5 Windows服务,它将使用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

1 个答案:

答案 0 :(得分: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人,但我认为静态字段初始化程序在方法中的任何其他代码之前运行,这一定是问题所在。

尝试在配置后立即在方法中实例化记录器。