.NET tracing in PowerShell without creating .config file

前端 未结 2 1331
日久生厌
日久生厌 2020-12-09 22:06

I know I can enable .NET tracing by adding element to App config (powershell.exe.config) in PowerShell installation folder. This is c

相关标签:
2条回答
  • 2020-12-09 22:53

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

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

    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();
    
    0 讨论(0)
  • 2020-12-09 22:55

    Yes you can write tracing in code.

    Trace.Write("Hello world");
    

    If you want something more advanced try Log4Net which generates a config file.

    0 讨论(0)
提交回复
热议问题