Skip to content

Commit 6e6355b

Browse files
committed
Added WriteToConsole option back, fixed some documentation issues
1 parent 4f7c933 commit 6e6355b

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

documentation/Start-PnPTraceLog.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Starts log tracing
1515
## SYNTAX
1616

1717
```powershell
18-
Start-PnPTraceLog [-Path <String>] [-Level <LogLevel>] [-AutoFlush <Boolean>]
18+
Start-PnPTraceLog [-Path <String>] [-Level <LogLevel>] [-AutoFlush <Boolean>] [-WriteToConsole <SwitchParameter>]
1919
```
2020

2121

@@ -36,7 +36,14 @@ This turns on trace logging to the file 'TraceOutput.txt' and will capture event
3636
Start-PnPTraceLog -Path ./TraceOutput.txt -Level Debug
3737
```
3838

39-
This turns on trace logging to the file 'TraceOutput.txt' and will capture debug events.
39+
This turns on trace logging to the file 'TraceOutput.txt' and will capture all events.
40+
41+
### EXAMPLE 3
42+
```powershell
43+
Start-PnPTraceLog -WriteToConsole -Level Debug
44+
```
45+
46+
This turns on trace logging to console in which you are running your PowerShell script and will capture all events.
4047

4148
## PARAMETERS
4249

@@ -45,11 +52,11 @@ Auto flush the trace log. Defaults to true.
4552

4653
```yaml
4754
Type: Boolean
48-
Parameter Sets: On
55+
Parameter Sets: (All)
4956

5057
Required: False
5158
Position: Named
52-
Default value: None
59+
Default value: True
5360
Accept pipeline input: False
5461
Accept wildcard characters: False
5562
```
@@ -59,12 +66,12 @@ The level of events to capture. Possible values are 'Debug', 'Error', 'Warning',
5966
6067
```yaml
6168
Type: LogLevel
62-
Parameter Sets: On
69+
Parameter Sets: (All)
6370
Accepted values: Debug, Error, Warning, Information
6471

6572
Required: False
6673
Position: Named
67-
Default value: None
74+
Default value: Information
6875
Accept pipeline input: False
6976
Accept wildcard characters: False
7077
```
@@ -74,7 +81,7 @@ The path and filename of the file to write the trace log to.
7481
7582
```yaml
7683
Type: String
77-
Parameter Sets: On
84+
Parameter Sets: (All)
7885

7986
Required: False
8087
Position: Named
@@ -83,7 +90,20 @@ Accept pipeline input: False
8390
Accept wildcard characters: False
8491
```
8592
86-
## RELATED LINKS
93+
### -WriteToConsole
94+
Write the trace log to the console.
95+
96+
```yaml
97+
Type: SwitchParameter
98+
Parameter Sets: (All)
99+
100+
Required: False
101+
Position: Named
102+
Default value: False
103+
Accept pipeline input: False
104+
Accept wildcard characters: False
105+
```
87106
88-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
107+
## RELATED LINKS
89108
109+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Base/StartTraceLog.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ public class StartTraceLog : PSCmdlet
1212
public string Path;
1313

1414
[Parameter(Mandatory = false)]
15-
public PnP.Framework.Diagnostics.LogLevel Level = PnP.Framework.Diagnostics.LogLevel.Information;
15+
public SwitchParameter WriteToConsole;
16+
17+
[Parameter(Mandatory = false)]
18+
public Framework.Diagnostics.LogLevel Level = Framework.Diagnostics.LogLevel.Information;
1619

1720
[Parameter(Mandatory = false)]
1821
public bool AutoFlush = true;
1922

2023
private const string FileListenername = "PNPPOWERSHELLFILETRACELISTENER";
2124
private const string ConsoleListenername = "PNPPOWERSHELLCONSOLETRACELISTENER";
25+
2226
protected override void ProcessRecord()
2327
{
24-
25-
// Setup Console Listener if Console switch has been specified or No file LogFile parameter has been set
26-
if (string.IsNullOrEmpty(Path))
28+
if (WriteToConsole.ToBool())
2729
{
2830
RemoveListener(ConsoleListenername);
29-
ConsoleTraceListener consoleListener = new ConsoleTraceListener(false);
30-
consoleListener.Name = ConsoleListenername;
31+
ConsoleTraceListener consoleListener = new(false)
32+
{
33+
Name = ConsoleListenername
34+
};
3135
Trace.Listeners.Add(consoleListener);
32-
PnP.Framework.Diagnostics.Log.LogLevel = Level;
36+
Framework.Diagnostics.Log.LogLevel = Level;
3337
}
3438

35-
// Setup File Listener
3639
if (!string.IsNullOrEmpty(Path))
3740
{
3841
RemoveListener(FileListenername);
@@ -42,19 +45,24 @@ protected override void ProcessRecord()
4245
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
4346
}
4447
// Create DelimitedListTraceListener in case Delimiter parameter has been specified, if not create TextWritterTraceListener
45-
TraceListener listener = new TextWriterTraceListener(Path);
46-
47-
listener.Name = FileListenername;
48+
TraceListener listener = new TextWriterTraceListener(Path)
49+
{
50+
Name = FileListenername
51+
};
4852
Trace.Listeners.Add(listener);
49-
PnP.Framework.Diagnostics.Log.LogLevel = Level;
53+
Framework.Diagnostics.Log.LogLevel = Level;
5054
}
5155

5256
Trace.AutoFlush = AutoFlush;
5357
Trace.IndentSize = 4;
54-
5558
}
5659

57-
private void RemoveListener(string listenerName)
60+
/// <summary>
61+
/// Tries to remove the listener with the given name from the Trace.Listeners collection.
62+
/// If the listener is not found, it will be ignored.
63+
/// </summary>
64+
/// <param name="listenerName">Name of the trace listener</param>
65+
private static void RemoveListener(string listenerName)
5866
{
5967
try
6068
{
@@ -70,7 +78,6 @@ private void RemoveListener(string listenerName)
7078
{
7179
// ignored
7280
}
73-
7481
}
7582
}
7683
}

0 commit comments

Comments
 (0)