Skip to content

Commit f7c3115

Browse files
authored
Merge pull request #4794 from KoenZomers/AddedInMemoryLogging
Added in memory logging option
2 parents 5118aa9 + 1016ce4 commit f7c3115

File tree

263 files changed

+1747
-878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

263 files changed

+1747
-878
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6060
- Added `Set-PnPSiteDocumentIdPrefix` which allows changing of the document id prefix on a site collection [#4765](https://github.com/pnp/powershell/pull/4765)
6161
- Added `Get-PnPMicrosoft365Roadmap` which allows retrieval of the Microsoft 365 Roadmap items [#4764](https://github.com/pnp/powershell/pull/4764)
6262
- Added `-Name` parameter to `Add-PnPApplicationCustomizer` cmdlet to allow for specifying the name of the application customizer [#4767](https://github.com/pnp/powershell/pull/4767)
63+
- Added `Get-PnPTraceLog` cmdlet which allows reading from the detailed in memory logs of the PnP PowerShell cmdlet execution [#4794](https://github.com/pnp/powershell/pull/4794)
6364

6465
### Changed
6566

documentation/Clear-PnPTraceLog.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
title: Clear-PnPTraceLog
4+
schema: 2.0.0
5+
applicable: SharePoint Online
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
online version: https://pnp.github.io/powershell/cmdlets/Clear-PnPTraceLog.html
8+
---
9+
10+
# Clear-PnPTraceLog
11+
12+
## SYNOPSIS
13+
Clears the log stream in memory
14+
15+
## SYNTAX
16+
17+
```powershell
18+
Clear-PnPTraceLog [-Verbose]
19+
```
20+
21+
## DESCRIPTION
22+
This clears the in memory stored log stream which was started with the [Start-PnPTraceLog -WriteToLogstream](Start-PnPTraceLog.md) cmdlet. It will not clear the log file if one was specified.
23+
24+
## EXAMPLES
25+
26+
### EXAMPLE 1
27+
```powershell
28+
Clear-PnPTraceLog
29+
```
30+
31+
This clears the in memory stored log stream
32+
33+
## PARAMETERS
34+
35+
### -Verbose
36+
When provided, additional debug statements will be shown while executing the cmdlet.
37+
38+
```yaml
39+
Type: SwitchParameter
40+
Parameter Sets: (All)
41+
42+
Required: False
43+
Position: Named
44+
Default value: None
45+
Accept pipeline input: False
46+
Accept wildcard characters: False
47+
```
48+
49+
## RELATED LINKS
50+
51+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Get-PnPTraceLog.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
title: Get-PnPTraceLog
4+
schema: 2.0.0
5+
applicable: SharePoint Online
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPTraceLog.html
8+
---
9+
10+
# Get-PnPTraceLog
11+
12+
## SYNOPSIS
13+
Returns logged messages during the execution of PnP PowerShell cmdlets
14+
15+
## SYNTAX
16+
17+
### Log from file
18+
19+
```powershell
20+
Get-PnPTraceLog -Path <string> [-Verbose]
21+
```
22+
### Log from log stream
23+
24+
```powershell
25+
Get-PnPTraceLog [-Verbose]
26+
```
27+
28+
## DESCRIPTION
29+
This cmdlet returns the logged messages during the execution of PnP PowerShell cmdlets. It can return the messages from an in memory log stream or from a file. Note that you cannot read from a log file if it is currently in use to write to. In this case, you would first have to stop logging to it using [Stop-PnPTraceLog](Stop-PnPTraceLog.md) and then read the log file. The in memory log stream is always available.
30+
31+
You can use [Start-PnPTraceLog](Start-PnPTraceLog.md) to start logging to a file and/or to an in memory stream.
32+
33+
## EXAMPLES
34+
35+
### EXAMPLE 1
36+
```powershell
37+
Get-PnPTraceLog
38+
```
39+
40+
This returns all items in the in memory stored log stream
41+
42+
### EXAMPLE 2
43+
```powershell
44+
Get-PnPTraceLog -Path "C:\temp\log.txt"
45+
```
46+
47+
This returns all items from the log file stored at the provided location. Note that you cannot read from a log file if it is currently in use to write to. In this case, you would first have to stop logging to it using [Stop-PnPTraceLog](Stop-PnPTraceLog.md) and then read the log file.
48+
49+
### EXAMPLE 3
50+
```powershell
51+
Get-PnPTraceLog | Where-Object { $_.Level -eq "Error" }
52+
```
53+
54+
This returns only logged items from the in memory stored log stream that have a level of "Error"
55+
56+
### EXAMPLE 4
57+
```powershell
58+
Get-PnPTraceLog | Where-Object { $_.CorrelationId -eq "5a6206a0-6c83-4446-9d1b-38c14f93cb60" }
59+
```
60+
61+
This returns only logged items from the in memory stored log stream that happened during the execution of a PnP PowerShell cmdlet with the provided correlation id. This is useful to find out what happened during the execution of a specific cmdlet. Mind that the correlation id is an unique identifier for the cmdlet execution assigned by PnP PowerShell and is not the same as the correlation id of a SharePoint operation.
62+
63+
### EXAMPLE 5
64+
```powershell
65+
Get-PnPTraceLog | Sort-Object -Property EllapsedMilliseconds -Descending -Top 10 | Select EllapsedMilliseconds, Source, Message
66+
```
67+
68+
Returns the 10 longest running operations from the in memory stored log stream. An operation is an action within the execution of a cmdlet. The output is sorted by the time it took to complete the operation with the longest execution at the top. The output shows the ellapsed time in milliseconds taken by a single operation, the cmdlet that was executed and the message that was logged.
69+
70+
### EXAMPLE 6
71+
```powershell
72+
Get-PnPTraceLog | Group-Object -Property CorrelationId | ForEach-Object { [pscustomobject]@{ Started = ($_.Group | Select -First 1).TimeStamp; Ended = ($_.Group | Select -Last 1).TimeStamp; Cmdlet = $_.Group[0].Source; TimeTaken = ($_.Group | Measure-Object -Property EllapsedMilliseconds -Sum).Sum; Logs = $_.Group }} | Sort-Object -Property TimeTaken -Descending -Top 5 | Select Started, Cmdlet, TimeTaken
73+
```
74+
75+
Returns the top 5 longest running cmdlets from the in memory stored log stream. The output is sorted by the TimeTaken property in descending order which shows the total execution time of a single cmdlet. The output contains the time the cmdlet started executing, the cmdlet that was executed and the total time it took to execute the cmdlet. From there it is easy to examine all the individual logs collected during the execution of that single cmdlet.
76+
77+
## PARAMETERS
78+
79+
### -Path
80+
The path to the log file. If not provided, the cmdlet will return the in memory log stream.
81+
82+
Note that you cannot read from a log file if it is currently in use to write to. In this case, you would first have to stop logging to it using [Stop-PnPTraceLog](Stop-PnPTraceLog.md) and then read the log file.
83+
84+
```yaml
85+
Type: String
86+
Parameter Sets: Log from file
87+
88+
Required: True
89+
Position: Named
90+
Default value: None
91+
Accept pipeline input: True
92+
Accept wildcard characters: False
93+
```
94+
95+
### -Verbose
96+
When provided, additional debug statements will be shown while executing the cmdlet.
97+
98+
```yaml
99+
Type: SwitchParameter
100+
Parameter Sets: (All)
101+
102+
Required: False
103+
Position: Named
104+
Default value: None
105+
Accept pipeline input: False
106+
Accept wildcard characters: False
107+
```
108+
109+
## RELATED LINKS
110+
111+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Start-PnPTraceLog.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ Starts log tracing
1515
## SYNTAX
1616

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

21-
2221
## DESCRIPTION
2322
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'.
2423

24+
You can look at the logged data using [Get-PnPTraceLog](Get-PnPTraceLog.md).
25+
26+
The logged data contains the following information in the following order:
27+
28+
- Timestamp
29+
- Source
30+
- Thread ID
31+
- Log level
32+
- Message
33+
- Elapsed time in milliseconds since the last log entry for the same cmdlet execution
34+
- Correlation ID which is an unique identifier per executed cmdlet so you can filter the log for everything logged during a specific cmdlet execution
35+
36+
Beware that the logged data can be quite verbose, especially when the level is set to 'Debug'. When logging in memory, it can take up a lot of memory. When logging to a file, it can take up a lot of disk space. So be careful when using this in production environments and only use it when you need to troubleshoot something or are aware of the consequences.
37+
2538
## EXAMPLES
2639

2740
### EXAMPLE 1
@@ -38,12 +51,19 @@ Start-PnPTraceLog -Path ./TraceOutput.txt -Level Debug
3851

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

54+
### EXAMPLE 3
55+
```powershell
56+
Start-PnPTraceLog -WriteToConsole -WriteToLogStream -Level Debug
57+
```
58+
59+
This turns on trace logging to the console and in memory stream in which you are running your PowerShell script and will capture all events.
60+
4161
### EXAMPLE 3
4262
```powershell
4363
Start-PnPTraceLog -WriteToConsole -Level Debug
4464
```
4565

46-
This turns on trace logging to console in which you are running your PowerShell script and will capture all events.
66+
This turns on trace logging to the console in which you are running your PowerShell script and will capture all events.
4767

4868
## PARAMETERS
4969

@@ -104,6 +124,20 @@ Accept pipeline input: False
104124
Accept wildcard characters: False
105125
```
106126
127+
### -WriteToLogStream
128+
Write the trace log to the in memory stream. Use [Get-PnPTraceLog](Get-PnPTraceLog.md) to read the log stream.
129+
130+
```yaml
131+
Type: SwitchParameter
132+
Parameter Sets: (All)
133+
134+
Required: False
135+
Position: Named
136+
Default value: False
137+
Accept pipeline input: False
138+
Accept wildcard characters: False
139+
```
140+
107141
## RELATED LINKS
108142
109143
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Stop-PnPTraceLog.md

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ online version: https://pnp.github.io/powershell/cmdlets/Set-PnPTraceLog.html
1010
# Stops-PnPTraceLog
1111

1212
## SYNOPSIS
13-
Stops log tracing and flushes the log buffer if any items in there.
13+
Stops all log tracing and flushes the log buffer if any items in there.
1414

1515
## SYNTAX
1616

1717
```powershell
18-
Stop-PnPTraceLog
18+
Stop-PnPTraceLog [-StopFileLogging <SwitchParameter>] [-StopConsoleLogging <SwitchParameter>] [-StopLogStreamLogging <SwitchParameter>] [-Verbose]
1919
```
2020

21-
2221
## DESCRIPTION
23-
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'.
22+
Stops PnP PowerShell tracelogging to specific targets. By default, all logging is stopped. You can use the parameters to stop specific logging targets only.
23+
24+
You can turn on the trace log with [Start-PnPTraceLog](Start-PnPTraceLog.md).
25+
You can look at the logged data using [Get-PnPTraceLog](Get-PnPTraceLog.md).
2426

2527
## EXAMPLES
2628

@@ -29,9 +31,61 @@ Stops .NET tracelogging. Many cmdlets output detailed trace information when exe
2931
Stop-PnPTraceLog
3032
```
3133

32-
This turns off trace logging
34+
This turns off all trace logging
3335

34-
## RELATED LINKS
36+
## EXAMPLES
37+
38+
### EXAMPLE 2
39+
```powershell
40+
Stop-PnPTraceLog -StopFileLogging -StopConsoleLogging
41+
```
42+
43+
This turns off trace logging to file and console, but keeps the other logging options active.
44+
45+
## PARAMETERS
46+
47+
### -StopConsoleLogging
48+
Allows you to specifically stop logging to the console while keeping the other logging options active.
49+
50+
```yaml
51+
Type: SwitchParameter
52+
Parameter Sets: (All)
3553

36-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
54+
Required: False
55+
Position: Named
56+
Default value: True
57+
Accept pipeline input: False
58+
Accept wildcard characters: False
59+
```
60+
61+
### -StopFileLogging
62+
Allows you to specifically stop logging to a file while keeping the other logging options active.
63+
64+
```yaml
65+
Type: SwitchParameter
66+
Parameter Sets: (All)
67+
68+
Required: False
69+
Position: Named
70+
Default value: True
71+
Accept pipeline input: False
72+
Accept wildcard characters: False
73+
```
74+
75+
### -StopLogStreamLogging
76+
Allows you to specifically stop logging to the in memory log stream while keeping the other logging options active.
77+
78+
```yaml
79+
Type: SwitchParameter
80+
Parameter Sets: (All)
81+
82+
Required: False
83+
Position: Named
84+
Default value: True
85+
Accept pipeline input: False
86+
Accept wildcard characters: False
87+
```
88+
89+
## RELATED LINKS
3790
91+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

0 commit comments

Comments
 (0)