diff --git a/documentation/Start-PnPTraceLog.md b/documentation/Start-PnPTraceLog.md index 49a4d16d9..a8d819815 100644 --- a/documentation/Start-PnPTraceLog.md +++ b/documentation/Start-PnPTraceLog.md @@ -15,7 +15,7 @@ Starts log tracing ## SYNTAX ```powershell -Start-PnPTraceLog [-Path ] [-Level ] [-AutoFlush ] +Start-PnPTraceLog [-Path ] [-Level ] [-AutoFlush ] [-WriteToConsole ] ``` @@ -36,7 +36,14 @@ This turns on trace logging to the file 'TraceOutput.txt' and will capture event Start-PnPTraceLog -Path ./TraceOutput.txt -Level Debug ``` -This turns on trace logging to the file 'TraceOutput.txt' and will capture debug events. +This turns on trace logging to the file 'TraceOutput.txt' and will capture all events. + +### EXAMPLE 3 +```powershell +Start-PnPTraceLog -WriteToConsole -Level Debug +``` + +This turns on trace logging to console in which you are running your PowerShell script and will capture all events. ## PARAMETERS @@ -45,11 +52,11 @@ Auto flush the trace log. Defaults to true. ```yaml Type: Boolean -Parameter Sets: On +Parameter Sets: (All) Required: False Position: Named -Default value: None +Default value: True Accept pipeline input: False Accept wildcard characters: False ``` @@ -59,12 +66,12 @@ The level of events to capture. Possible values are 'Debug', 'Error', 'Warning', ```yaml Type: LogLevel -Parameter Sets: On +Parameter Sets: (All) Accepted values: Debug, Error, Warning, Information Required: False Position: Named -Default value: None +Default value: Information Accept pipeline input: False Accept wildcard characters: False ``` @@ -74,7 +81,7 @@ The path and filename of the file to write the trace log to. ```yaml Type: String -Parameter Sets: On +Parameter Sets: (All) Required: False Position: Named @@ -83,7 +90,20 @@ Accept pipeline input: False Accept wildcard characters: False ``` -## RELATED LINKS +### -WriteToConsole +Write the trace log to the console. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` -[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) +## RELATED LINKS +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) \ No newline at end of file diff --git a/src/Commands/Base/StartTraceLog.cs b/src/Commands/Base/StartTraceLog.cs index 099fc4eed..605e408e9 100644 --- a/src/Commands/Base/StartTraceLog.cs +++ b/src/Commands/Base/StartTraceLog.cs @@ -12,27 +12,30 @@ public class StartTraceLog : PSCmdlet public string Path; [Parameter(Mandatory = false)] - public PnP.Framework.Diagnostics.LogLevel Level = PnP.Framework.Diagnostics.LogLevel.Information; + public SwitchParameter WriteToConsole; + + [Parameter(Mandatory = false)] + public Framework.Diagnostics.LogLevel Level = Framework.Diagnostics.LogLevel.Information; [Parameter(Mandatory = false)] public bool AutoFlush = true; private const string FileListenername = "PNPPOWERSHELLFILETRACELISTENER"; private const string ConsoleListenername = "PNPPOWERSHELLCONSOLETRACELISTENER"; + protected override void ProcessRecord() { - - // Setup Console Listener if Console switch has been specified or No file LogFile parameter has been set - if (string.IsNullOrEmpty(Path)) + if (WriteToConsole.ToBool()) { RemoveListener(ConsoleListenername); - ConsoleTraceListener consoleListener = new ConsoleTraceListener(false); - consoleListener.Name = ConsoleListenername; + ConsoleTraceListener consoleListener = new(false) + { + Name = ConsoleListenername + }; Trace.Listeners.Add(consoleListener); - PnP.Framework.Diagnostics.Log.LogLevel = Level; + Framework.Diagnostics.Log.LogLevel = Level; } - // Setup File Listener if (!string.IsNullOrEmpty(Path)) { RemoveListener(FileListenername); @@ -42,19 +45,24 @@ protected override void ProcessRecord() Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path); } // Create DelimitedListTraceListener in case Delimiter parameter has been specified, if not create TextWritterTraceListener - TraceListener listener = new TextWriterTraceListener(Path); - - listener.Name = FileListenername; + TraceListener listener = new TextWriterTraceListener(Path) + { + Name = FileListenername + }; Trace.Listeners.Add(listener); - PnP.Framework.Diagnostics.Log.LogLevel = Level; + Framework.Diagnostics.Log.LogLevel = Level; } Trace.AutoFlush = AutoFlush; Trace.IndentSize = 4; - } - private void RemoveListener(string listenerName) + /// + /// Tries to remove the listener with the given name from the Trace.Listeners collection. + /// If the listener is not found, it will be ignored. + /// + /// Name of the trace listener + private static void RemoveListener(string listenerName) { try { @@ -70,7 +78,6 @@ private void RemoveListener(string listenerName) { // ignored } - } } } \ No newline at end of file