Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions documentation/Start-PnPTraceLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Starts log tracing
## SYNTAX

```powershell
Start-PnPTraceLog [-Path <String>] [-Level <LogLevel>] [-AutoFlush <Boolean>]
Start-PnPTraceLog [-Path <String>] [-Level <LogLevel>] [-AutoFlush <Boolean>] [-WriteToConsole <SwitchParameter>]
```


Expand All @@ -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

Expand All @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
Expand All @@ -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)
37 changes: 22 additions & 15 deletions src/Commands/Base/StartTraceLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
/// <summary>
/// Tries to remove the listener with the given name from the Trace.Listeners collection.
/// If the listener is not found, it will be ignored.
/// </summary>
/// <param name="listenerName">Name of the trace listener</param>
private static void RemoveListener(string listenerName)
{
try
{
Expand All @@ -70,7 +78,6 @@ private void RemoveListener(string listenerName)
{
// ignored
}

}
}
}
Loading