Nlog: Nlog run in Windows Service mode, throw exception.

Created on 20 Nov 2018  ·  5Comments  ·  Source: NLog/NLog

NLog version:

    <PackageReference Include="NLog" Version="4.5.11"/>
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0"/>

Platform: .NET Core 2

Current NLog config (xml)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Error" throwConfigExceptions="true">
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>
    <targets>
        <target name="blackhole" xsi:type="Null" />
        <target 
            name="logfile" 
            xsi:type="File" 
            autoFlush="true" 
            encoding="utf-8" 
            writeBom="false"
            maxArchiveFiles="10"
            fileName="${basedir}/${logger}/${aspnet-MVC-Action}/${date:format=yyyy-MM-dd}.txt" 
            layout="${time:format=HH:mm:ss.fff} [${uppercase:${level}}] ${message} ${exception}"/>
        <target 
            name="logconsole" 
            useDefaultRowHighlightingRules="true" 
            xsi:type="ColoredConsole" 
            encoding="utf-8"
            layout="${time:format=HH:mm:ss.fff} [${uppercase:${level}}] ${message} ${exception}"/>
    </targets>
    <rules>
        <logger name="Microsoft.AspNet.OData.*" minlevel="Trace" writeTo="logconsole" />
        <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
        <logger name="*" minlevel="Trace" writeTo="logfile"/>
        <logger name="*" minlevel="Trace" writeTo="logconsole"/>
    </rules>
</nlog>
  • What is the current result?
    ```C#
    //If run in WindowsService mode,when run this code:
    Logger logger = NLog.Web.NLogBuilder.ConfigureNLog("Nlog.config").GetCurrentClassLogger();
    //it throw exception.
- What is the expected result? `No any exception.`
- Did you checked the [Internal log](https://github.com/NLog/NLog/wiki/Internal-Logging)? `no`
- Please post full exception details

at System.ConsolePal.SetConsoleOutputEncoding(Encoding enc)
at System.Console.set_OutputEncoding(Encoding value)
at NLog.Targets.ColoredConsoleTarget.InitializeTarget()
at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
at NLog.Targets.Target.NLog.Internal.ISupportsInitialize.Initialize(LoggingConfiguration configuration)
at NLog.Config.LoggingConfiguration.InitializeAll()
at NLog.LogFactory.ReconfigExistingLoggers()
at NLog.LogFactory.set_Configuration(LoggingConfiguration value)
at NLog.LogFactory.LoadConfiguration(String configFile)
at NLog.LogManager.LoadConfiguration(String configFile)
at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)
at UpdateECP.Program.Main(String[] args) in D:\HLST\DotNetCore\UpdateECP\Program.cs:line 33

InternalLoggerInfo:

2018-11-20 17:20:58.6562 Info Auto loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Extensions.Logging.dll
2018-11-20 17:20:58.6562 Info Loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Extensions.Logging.dll
2018-11-20 17:20:58.7246 Info NLog.Extensions.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 1.3.0.804. Product version: 1.3.0.
2018-11-20 17:20:58.7667 Info Auto loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Extensions.Logging.dll succeeded!
2018-11-20 17:20:58.7667 Info Auto loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Web.AspNetCore.dll
2018-11-20 17:20:58.7667 Info Loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Web.AspNetCore.dll
2018-11-20 17:20:58.7809 Info NLog.Web.AspNetCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.7.930. Product version: 4.7.
2018-11-20 17:20:58.7809 Info Auto loading assembly file: D:\HLST\DotNetCore\UpdateECP\release\NLog.Web.AspNetCore.dll succeeded!
2018-11-20 17:20:59.1888 Error ColoredConsole Target[logconsole]: Error initializing target Exception: System.IO.IOException: 句柄无效。
at System.ConsolePal.SetConsoleOutputEncoding(Encoding enc)
at System.Console.set_OutputEncoding(Encoding value)
at NLog.Targets.ColoredConsoleTarget.InitializeTarget()
at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
```

  • Are there any workarounds? no
  • Is there a version in which it did work? uncertain
question

All 5 comments

I try run in NETWROKSERVICE, SYSTEM, LOCALSYSTEM user, but still throw exception.

May be issue is windows service can not open console, but how to process it.

Looks like a duplicate of https://github.com/dotnet/corefx/issues/19587 and https://github.com/dotnet/corefx/issues/21483

I recommend that you remove encoding="utf-8" for the ColoredConsole-target in the NLog.config

Have updated #3018 to include better handling of Exceptions when trying to change
Console.OutputEncoding on a platform with missing support.

I removed encoding="utf-8", that worked.

I try to set Console.OutputEncoding but throw exception for any encoding.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sszost picture sszost  ·  3Comments

carkov1990 picture carkov1990  ·  3Comments

ErcinDedeoglu picture ErcinDedeoglu  ·  3Comments

geedsen picture geedsen  ·  3Comments

Jerefeny picture Jerefeny  ·  3Comments