PowerShell 中的 .NET 跟踪,无需创建 .config 文件 [英] .NET tracing in PowerShell without creating .config file

查看:37
本文介绍了PowerShell 中的 .NET 跟踪,无需创建 .config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以启用 .NET 跟踪 通过添加 element 到 PowerShell 安装文件夹中的应用配置 (powershell.exe.config).这在 PowerShell 中的 System.Net 跟踪中有介绍.

I know I can enable .NET tracing by adding <system.diagnostics> element to App config (powershell.exe.config) in PowerShell installation folder. This is covered in System.Net tracing in PowerShell.

事实上,我也想记录 System.Net 跟踪源(例如 FtpWebRequest).

In fact, I too want to log System.Net tracing source (e.g. FtpWebRequest).

但是有没有办法在本地启用跟踪?就像代码本身一样?或者可能使用一些命令行开关?或者我可以至少将应用配置文件放在 local 文件夹中,而不必修改系统范围的设置?

But is there a way to enable tracing locally? Like in the code itself? Or possibly using some command-line switch? Or can I at least have the App config file in a local folder, not to have to modify the system-wide settings?

推荐答案

只需在代码中(因此在 Powershell 中)启用默认跟踪源(Trace.Information 等)相对容易.

Just enabling the default trace sources (Trace.Information etc.) in code (and therefore in Powershell) is relatively easy.

System.Net 跟踪源执行此操作更为复杂,因为它们不可公开访问.

Doing so for the System.Net trace sources is more complicated because they are not publicly accessible.

我以前在 C# 中看到过,调用 System.Net 方法,例如Dns.Resolve 是创建 TraceSource 所必需的,但在 Powershell 中似乎不需要.

I have previously seen that in C#, calling a System.Net method e.g. Dns.Resolve was necessary in order to get the TraceSource to be created but this doesn't seem to be needed in Powershell.

所以不是一个很好的解决方案......但这取决于你的替代方案是什么:

So not a great solution... but it depends what your alternatives are I guess:

$id = [Environment]::TickCount;
$fileName = "${PSScriptRoot}\Powershell_log_${id}.txt"
$listener1 = [System.Diagnostics.TextWriterTraceListener]::New($fileName, "text_listener")
$listener2 = [System.Diagnostics.ConsoleTraceListener]::New()
$listener2.Name = "console_listener"

[System.Diagnostics.Trace]::AutoFlush = $true
[System.Diagnostics.Trace]::Listeners.Add($listener1) | out-null
[System.Diagnostics.Trace]::Listeners.Add($listener2) | out-null

# Use reflection to enable and hook up the TraceSource
$logging = [System.Net.Sockets.Socket].Assembly.GetType("System.Net.Logging")
$flags = [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static
$logging.GetField("s_LoggingEnabled", $flags).SetValue($null, $true)
$webTracing = $logging.GetProperty("Web", $flags);
$webTraceSource = [System.Diagnostics.Tracesource]$webTracing.GetValue($null, $null);
$webTraceSource.Switch.Level = [System.Diagnostics.SourceLevels]::Information
$webTracesource.Listeners.Add($listener1) | out-null
$webTracesource.Listeners.Add($listener2)  | out-null

[System.Diagnostics.Trace]::TraceInformation("About to do net stuff");
[System.Net.FtpWebRequest]::Create("ftp://www.google.com") | out-null
[System.Diagnostics.Trace]::TraceInformation("Finished doing net stuff");

#get rid of the listeners
[System.Diagnostics.Trace]::Listeners.Clear();
$webTraceSource.Listeners.Clear();
$listener1.Dispose();
$listener2.Dispose();

这篇关于PowerShell 中的 .NET 跟踪,无需创建 .config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆