在PowerShell中进行.NET跟踪而无需创建.config文件 [英] .NET tracing in PowerShell without creating .config file

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

问题描述

我知道我可以启用 <system.diagnostics>元素到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方法.要创建TraceSource,必须使用Dns.Resolve,但这在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天全站免登陆