在 PowerShell 中使用 NLog-Logger 时禁用日志记录

Logging disabled when using NLog-Logger in PowerShell

我的nlog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="c:\temp\console-example-internal.log"
      internalLogLevel="Info" >

    <!-- the targets to write to -->
    <targets>
        <target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
                layout="${shortdate} | ${level} | ${callsite} | ${message} | ${all-event-properties} ${exception:format=tostring}" />
        <target xsi:type="Console" name="logconsole"
                layout="${shortdate} | ${level} | ${callsite} | ${message} | ${all-event-properties} ${exception:format=tostring}" />
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <logger name="*" minlevel="Trace" writeTo="logfile,logconsole" />
    </rules>
</nlog>

当我在 PowerShell 中导入 Nlog.dll 时,来自 LogManager.GetCurrentClassLogger() 的记录器被禁用。所以我不能登录。

在 PowerShell 中必须手动设置 nlog.config。 https://gist.github.com/RafPe/95ef838c51edb7ef43c4

$xmlConfig = New-Object NLog.Config.XmlLoggingConfiguration("\pathToConfig\NLog.config")
[NLog.LogManager]::Configuration = $xmlConfig
    
# Create logger 
$logger = [NLog.LogManager]::GetLogger('logger.name')