NLog用于Powershell

问题描述

我开发了一个相当长的ps1脚本,因此我决定从Write- *转向使用Nlog进行日志记录。 它工作得很好,但是比我意识到我想保留控制台中的文字,因为如果不进行疑难杂症,它会更加友好。 因此,我添加一个新的目标(尝试ConsoleTargetColoredConsoleTarget)以查看在转到NLog之前看到的内容。 不幸的是我什么也没看见。

这是我的NLog.config(非常适合文件目标):

<?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"
      autoReload="true">
  <targets async="true">
    <!-- text file - absolute directory e.g. not using "basedir" variable -->
    <target name="logfile"
            xsi:type="File"
            fileName="C:\logfile.txt"
            layout="${longdate}|${message} ${exception:format=tostring}"/>
    <target xsi:type="ColoredConsole"
          name="ColoredConsole"
          layout="${longdate}|${message} ${exception:format=tostring}"
          useDefaultRowHighlightingRules="true"
          errorStream="true"
          detectConsoleAvailable="true"
          detectOutputRedirected="true" />
  </targets>
  <rules>
    <!-- log only info and above -->
    <logger name="*" minLevel="Trace" writeto="logfile"/>
    <logger name="*" minLevel="Trace" writeto="ColoredConsole"/>
  </rules>
</nlog>

一旦启动脚本(在这种情况下为批处理),日志文件就可以很好地工作,但是控制台保持空白。

enter image description here

想法?

谢谢!

解决方法

我发现了问题。 我正在运行的NLog版本是4.5.4。 我正在使用的一个属性(detectOutputRedirected)仅> = 4.6受支持。

先生解决了!