Skip to content

Commit

Permalink
Updated tracelogging cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvanhunen committed Dec 4, 2024
1 parent 6745d9a commit 50fe5e6
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 281 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-Force` parameter to `Set-PnPPropertyBagValue` cmdlet to toggle NoScript status of the site.
- Added `-Batch` parameter to `Invoke-PnPGraphMethod` cmdlet to allow adding request in a batch.
- Added `-List` parameter to `Get-PnPFolderItem`, `Get-PnPFileInFolder` and `Get-PnPFolderInFolder` which allows them to work with a document library containing more than 5,000 items [#4611](https://github.com/pnp/powershell/pull/4611)
- Added `Start-PnPTraceLog`, `Stop-PnPTraceLog` and `Get-PnPTraceLog` cmdlets to handle tracelogging. Removed `Set-PnPTraceLog` cmdlet.

### Changed

Expand Down Expand Up @@ -111,6 +112,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed `-WebLogin` parameter from `Connect-PnPOnline` cmdlet. It was marked obsolete and was a security risk.
- Removed `Set-PnPMinimalDownloadStrategy` as it's not applicable anymore to SharePoint Online. If you need the functionality you can always turn on the feature with `Enable-PnPFeature -Id 87294c72-f260-42f3-a41b-981a2ffce37a` or turn it off with `Disable-PnPFeature -Id 87294c72-f260-42f3-a41b-981a2ffce37a`
- Removed `-SPOManagementShell` parameter from `Connect-PnPOnline` cmdlet. It reduces the risk of changes coming from Microsoft. Use your own Entra ID app instead.
- Removed `Set-PnPTraceLog` cmdlet and introduced `Start-PnPTraceLog` and `Stop-PnPTraceLog` with similar parameters.

### Contributors

Expand Down
1 change: 1 addition & 0 deletions MIGRATE-2.0-to-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Recommend referring to these 2 links:
| Register-PnPEntraIDAppForInteractiveLogin | Removed `-LaunchBrowser`, `-NoPopup` and credential based auth. The default auth method is now Interactive.|
| Set-PnPMinimalDownloadStrategy | Removed cmdlet. If you need the functionality you can always turn on the feature with `Enable-PnPFeature -Id 87294c72-f260-42f3-a41b-981a2ffce37a` or turn it off with `Disable-PnPFeature -Id 87294c72-f260-42f3-a41b-981a2ffce37a` |
| Connect-PnPOnline | Removed `-SPOManagementShell` option for authentication. It reduces the risk of changes from Microsoft which can potentially break the scripts . Use your own Entra ID app instead via `-ClientId` parameter. |
| Set-PnPTraceLog | Removed cmdlet and introduced `Start-PnPTraceLog` and `Stop-PnPTracelog` with similar parameters and functionality |

## Other notable changes

Expand Down
179 changes: 0 additions & 179 deletions documentation/Set-PnPTraceLog.md

This file was deleted.

89 changes: 89 additions & 0 deletions documentation/Start-PnPTraceLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
Module Name: PnP.PowerShell
title: Set-PnPTraceLog
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPTraceLog.html
---

# Start-PnPTraceLog

## SYNOPSIS
Starts log tracing

## SYNTAX

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


## DESCRIPTION
Starts .NET tracelogging. Many cmdlets output detailed trace information when executed. Turn on the trace log with this cmdlet, optionally specify the level. By default the level is set to 'Information', but you will receive more detail by setting the level to 'Debug'.

## EXAMPLES

### EXAMPLE 1
```powershell
Set-PnPTraceLog -Path ./TraceOutput.txt
```

This turns on trace logging to the file 'TraceOutput.txt' and will capture events of at least 'Information' level.

### EXAMPLE 2
```powershell
Set-PnPTraceLog -Path ./TraceOutput.txt -Level Debug
```

This turns on trace logging to the file 'TraceOutput.txt' and will capture debug events.

## PARAMETERS

### -AutoFlush
Auto flush the trace log. Defaults to true.

```yaml
Type: Boolean
Parameter Sets: On

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Level
The level of events to capture. Possible values are 'Debug', 'Error', 'Warning', 'Information'. Defaults to 'Information'.
```yaml
Type: LogLevel
Parameter Sets: On
Accepted values: Debug, Error, Warning, Information

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Path
The path and filename of the file to write the trace log to.
```yaml
Type: String
Parameter Sets: On

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
37 changes: 37 additions & 0 deletions documentation/Stop-PnPTraceLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
Module Name: PnP.PowerShell
title: Set-PnPTraceLog
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPTraceLog.html
---

# Stops-PnPTraceLog

## SYNOPSIS
Stops log tracing and flushes the log buffer if any items in there.

## SYNTAX

```powershell
Stop-PnPTraceLog
```


## DESCRIPTION
Stops .NET tracelogging. Many cmdlets output detailed trace information when executed. Turn on the trace log with Start-PnPTraceLog, optionally specify the level. By default the level is set to 'Information', but you will receive more detail by setting the level to 'Debug'.

## EXAMPLES

### EXAMPLE 1
```powershell
Stop-PnPTraceLog
```

This turns off trace logging

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

3 changes: 2 additions & 1 deletion src/Commands/Base/BasePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class BasePSCmdlet : PSCmdlet
{
protected override void BeginProcessing()
{
Framework.Diagnostics.Log.Debug("PnPPowerShell", $"Executing {MyInvocation.MyCommand.Name}");
base.BeginProcessing();
if (MyInvocation.MyCommand.Name.ToLower() != MyInvocation.InvocationName.ToLower())
{
Expand Down Expand Up @@ -72,7 +73,7 @@ protected override void ProcessRecord()
TokenHandler.EnsureRequiredPermissionsAvailableInAccessTokenAudience(this, gex.AccessToken);
}
}
if(string.IsNullOrWhiteSpace(errorMessage) && gex.HttpResponse != null && gex.HttpResponse.StatusCode == System.Net.HttpStatusCode.Forbidden)
if (string.IsNullOrWhiteSpace(errorMessage) && gex.HttpResponse != null && gex.HttpResponse.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
errorMessage = "Access denied. Check for the required permissions.";
}
Expand Down
39 changes: 39 additions & 0 deletions src/Commands/Base/GetTraceLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Microsoft.PowerShell.Cmdletization.Xml;
using PnP.PowerShell.Model;

namespace PnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsCommon.Get, "PnPTraceLog")]
public class GetTraceLog : PSCmdlet
{
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, ParameterSetName = "On")]
public string Path;

protected override void ProcessRecord()
{
if (!System.IO.Path.IsPathRooted(Path))
{
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
}
if (File.Exists(Path))
{
var lines = System.IO.File.ReadAllLines(Path);
foreach (var line in lines)
{
var items = line.Split('\t');
WriteObject(new TraceLogEntry(items), true);
}
} else {
throw new PSArgumentException($"File {Path} does not exist");
}
}


}
}
Loading

0 comments on commit 50fe5e6

Please sign in to comment.