'Logging disabled when using NLog-Logger in PowerShell

My 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>

enter image description here

When i import the Nlog.dll in PowerShell the logger from LogManager.GetCurrentClassLogger() is disabled. Therefore i can't log.



Solution 1:[1]

In PowerShell one has to set the nlog.config manually. 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')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rubgra