Andrew
Andrew

Reputation: 477

NLog not logging (Windows Service)

I have a windows service that I am converting from my own logging to NLog. I have used NLog in a previous windows service application and felt that I knew what I was doing. But it isn't logging :(

I have the following NLog Configuration:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="On" internalLogFile="c:\temp\nlog-internal.log" >

  <variable name="DebugInfoLayout" value="[${date:format=MM/dd/yyyy h\:mm\:ss tt}] [${gdc:item=location}]  |  ${level}  |  ${message}" />
  <variable name="InfoLayout" value="[${date:format=MM/dd/yyyy h\:mm\:ss tt}] ${gdc:item=SoftwareName} Version ${gdc:item=SoftwareVersion}  -  ${message}" />
  <variable name="LogLayout" value="[${date:format=MM/dd/yyyy h\:mm\:ss tt}] ${message}" />
  <variable name="logDir" value="${basedir}/LogFiles" />
  <variable name="ArchiveDir" value="${basedir}/LogFiles/Archive" />

  <targets async="true">
    <target name="Errors" xsi:type="File" fileName="${logDir}/errors.log" layout="${LogLayout}" keepFileOpen="false" archiveFileName="${ArchiveDir}/errors_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30" archiveOldFileOnStartup="true" />
    <target name="Info" xsi:type="File" fileName="${logDir}/info.log" layout="${InfoLayout}"  keepFileOpen="false" archiveFileName="${ArchiveDir}/info_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30"/>
    <target name="Debug" xsi:type="File" fileName="${logDir}/debug.log" layout="${DebugInfoLayout}"  keepFileOpen="false" archiveFileName="${ArchiveDir}/debug_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30" />  
  </targets>

  <rules>
    <logger name="Errors" minlevel="Trace" maxlevel="Fatal" writeTo="Errors" />
    <logger name="Info" minlevel="Trace" maxlevel="Warn" writeTo="Info" />
    <logger name="Debug" minlevel="Trace" maxlevel="Fatal" writeTo="Debug" />
  </rules>
</nlog>    

To me, this looks correct. And it is the same as in my other application. From the code perspective, I have this in my service.vb file:

'NLog Instances
Dim errorLogger As NLog.Logger = NLog.LogManager.GetLogger("Errors")
Dim infoLogger As NLog.Logger = NLog.LogManager.GetLogger("Info")
Dim debugLogger As NLog.Logger = NLog.LogManager.GetLogger("Debug")

Then lower in the subroutines:

    Try
    Catch ex As Exception
        WritetoNLog(errorlogger, NLog.LogLevel.Error, System.Reflection.MethodInfo.GetCurrentMethod.Name, ex.Message)
        WritetoNLog(errorlogger, NLog.LogLevel.Error, System.Reflection.MethodInfo.GetCurrentMethod.Name, ex.InnerException.ToString)
    End Try

And then for that simple routine:

Public Sub WritetoNLog(ByRef logger As NLog.Logger, ByVal nlogtype As NLog.LogLevel, ByVal location As String, message As String)
    NLog.GlobalDiagnosticsContext.Set("location", String.Format("{0,-35}", location))

    Select Case nlogtype
        Case NLog.LogLevel.Trace
            logger.Trace(message)
        Case NLog.LogLevel.Fatal
            logger.Fatal(message)
        Case NLog.LogLevel.Info
            logger.Info(message)
        Case NLog.LogLevel.Debug
            logger.Debug(message)
        Case NLog.LogLevel.Error
            logger.Error(message)
        Case NLog.LogLevel.Warn
            logger.Warn(message)
    End Select

    NLog.LogManager.Flush()

End Sub

Upvotes: 0

Views: 1393

Answers (1)

Andrew
Andrew

Reputation: 477

This was a mistake internalLogLevel="On" should have been internalLogLevel="Trace" (or similar).

Upvotes: 1

Related Questions