繁体   English   中英

写入调试消息可以传输到日志文件吗?

[英]Can Write-debug messages be transferred to Log file?

详细写消息不会重定向到Out-File。

我的高级功能失败,没有任何跟踪,因此我想放置调试消息。 执行脚本时,它将不会在控制台中运行,因此我看不到该消息。

如果消息可以传输到日志文件,那么我可以看到它。 Write-debug是否可以将值传递到日志文件?

如果您使用的是Powershell v3 +,则可以将调试流重定向到文本文件:

样本文件:test.ps1

$debugPreference = "Continue"
Write-Output "Output msg"
Write-Debug "Debug msg"

在powershell命令行中:

c:\temp\test.ps1 5>c:\debug.txt

c:\\ debug.txt然后显示一行“ Debug msg”

5> c:\\ debug.txt有点晦涩,因此让我们对其进行分解。 “ 5”表示调试流,这是此处此处所述的第5个固有Powershell流。 然后> c:\\ debug.txt告诉它将所有调试输出重定向到该文件。

您还可以使用5>&1进行播放,这会将调试流重定向到普通的stdout。

如果您使用的是Powershell v2,则可以查看提供良好日志记录的已编译模块 ,也可以使用此处所述的脚本本地函数,类似于此:

$DebuglogPreference = 'Continue'
function Write-DebugLog 
{
   param( [string]$message, [string]$filepath = 'c:\debug.txt' )

   if ($DebuglogPreference -eq 'Continue') 
   {
       $message | Out-File $filepath -append
   }
}

Write-DebugLog "Debug msg"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM