Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process ID (PID) of StoreBroker PowerShell session now captured in log #62

Merged
merged 1 commit into from
Sep 29, 2017

Conversation

HowardWolosky
Copy link
Member

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 exactly
was happening 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

@@ -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:SBLogPid`** [bool] Defaults to `$false`. If `$true`, the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name by itself sounds like a numerical value, or a variable for a Pid to use for logging. Maybe 'SBShouldLogPid'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable.

$logFileMessage = '{0}{1} : [{2}]{3}: {4} : {5} : {6}' -f
(" " * $Indent),
$dateString,
$pid,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$pid is the same syntax we use for local variables. Can we go with $global:PID?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

if ($global:SBLogPid)
{
$MaxPidDigits = 10 # This is an estimate (see https://stackoverflow.com/questions/17868218/what-is-the-maximum-process-id-on-windows)
$pidPadding = $MaxPidDigits - "$pid".length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be format specifiers for adding appropriate padding. Would save you a variable and some logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS C:> $MaxPidDigits = 10
PS C:> "'{0,$MaxPidDigits}'" -f 12341
' 12341'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, GitHub removed the extra spaces, but you get the idea :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While interesting, that doesn't get me the formatting that I want. I want the padding on the end, not the beginning.

$Message
if ($global:SBLogPid)
{
$MaxPidDigits = 10 # This is an estimate (see https://stackoverflow.com/questions/17868218/what-is-the-maximum-process-id-on-windows)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this have $maxPidDigits syntax?

Since we're estimating, it seems safer to go with:
$maxPidDigits = [Math]::Max(10, "$global:PID".Length)

Just in case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering this a constant, hence SentenceCase as opposed to camelCase (for a varaible). Your suggestion on using Max for this scenario sounds good though.

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)
@HowardWolosky HowardWolosky merged commit e55f267 into microsoft:master Sep 29, 2017
@HowardWolosky HowardWolosky deleted the logPid branch September 29, 2017 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants