Skip to content

Commit

Permalink
Process ID (PID) of StoreBroker PowerShell session now captured in log
Browse files Browse the repository at this point in the history
Some users have scenarios where they are running concurrent instances of StoreBroker,
and for reasons specific to their situation, have chosen to have the logs always go
to the same log file (instead of using `$global:SBLogPath` to set a different
log file for each session.

In these scenarios, it can be challenging to read the logs and understand what was
happening exactly since there are entries from multiple commands intermixed.  This
change introduces adding the `$pid` of the StoreBroker PowerShell session to every
log entry so that on post-mortem analysis, it would be easy to filter to just the
logs for the specific `$pid`.

For more info on `$pid`, [see here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-5.1&viewFallbackFrom=powershell-Microsoft.PowerShell.Core#pid)
  • Loading branch information
HowardWolosky committed Sep 29, 2017
1 parent ff2db88 commit e55f267
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
12 changes: 12 additions & 0 deletions Documentation/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ when the module is loaded.
When `$true`, times are logged using UTC (and those timestamps will end with a Z per the
[W3C standard](http://www.w3.org/TR/NOTE-datetime))

**`$global:SBShouldLogPid`** [bool] Defaults to `$false`. If `$true`, the
Process ID (`$global:PID`) of the current PowerShell process will be added
to every log entry. This can be helpful if you have situations where
multiple instances of StoreBroker run concurrently and you want to
more easily isolate the log entries for one process. An alternative
solution would be to use `$global:SBLogPath` to specify a different
log file for each StoreBroker process. An easy way to view the filtered
entries for a session is (replacing `PID` with the PID that you are interested in):

Get-Content -Path $global:SBLogPath -Encoding UTF8 | Where { $_ -like '*[[]PID[]]*' }


> **PowerShell Tip**
>
> If you wish to always use a different value for these, set the new values in your PowerShell
Expand Down
34 changes: 27 additions & 7 deletions StoreBroker/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ function Initialize-HelpersGlobalVariables
}
}

if (!(Get-Variable -Name SBShouldLogPid -Scope Global -ValueOnly -ErrorAction SilentlyContinue))
{
$global:SBShouldLogPid = $false
}

if (!(Get-Variable -Name SBNotifyDefaultDomain -Scope Global -ValueOnly -ErrorAction SilentlyContinue))
{
$global:SBNotifyDefaultDomain = $null
Expand Down Expand Up @@ -483,19 +488,34 @@ function Write-Log
$dateString = $date.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ssZ")
}

$logFileMessage = '{0}{1} : {2} : {3} : {4}' -f
(" " * $Indent),
$dateString,
$env:username,
$Level.ToUpper(),
$Message

$consoleMessage = '{0}{1} : {2} : {3}' -f
(" " * $Indent),
$dateString,
$env:username,
$Message

if ($global:SBShouldLogPid)
{
$MaxPidDigits = 10 # This is an estimate (see https://stackoverflow.com/questions/17868218/what-is-the-maximum-process-id-on-windows)
$pidColumnLength = $MaxPidDigits + "[]".Length
$logFileMessage = "{0}{1} : {2, -$pidColumnLength} : {3} : {4} : {5}" -f
(" " * $Indent),
$dateString,
"[$global:PID]",
$env:username,
$Level.ToUpper(),
$Message
}
else
{
$logFileMessage = '{0}{1} : {2} : {3} : {4}' -f
(" " * $Indent),
$dateString,
$env:username,
$Level.ToUpper(),
$Message
}

switch ($Level)
{
'Error' { Write-Error $consoleMessage }
Expand Down
2 changes: 1 addition & 1 deletion StoreBroker/StoreBroker.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
CompanyName = 'Microsoft Corporation'
Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.'

ModuleVersion = '1.9.0'
ModuleVersion = '1.10.0'
Description = 'Provides command-line access to the Windows Store Submission REST API.'

RootModule = 'StoreIngestionApi'
Expand Down

0 comments on commit e55f267

Please sign in to comment.