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

Improve use of Write-Log and exceptions #97

Merged
merged 9 commits into from
Jan 30, 2018
Merged

Improve use of Write-Log and exceptions #97

merged 9 commits into from
Jan 30, 2018

Conversation

danbelcher-MSFT
Copy link
Contributor

Previously, users of Write-Log were left to handle creating messages themselves when logging an exception. Typically, our use has been to extract the exception's message and log this string. The issue with this approach is that we lose other valuable information, such as the originating script, line, exception type, etc.

With this change, Write-Log now accepts a -Exception parameter and handles logging the full exception information. This new functionality works in combination with the existing -Message parameter, so that users can log some context before logging the exception information. We hope that this change will make debugging StoreBroker issues faster and more intuitive for the reader.

Additional changes:

  • Write-Log properly handles pipelining multiple strings to the -Message parameter.
  • Existing uses of Write-Log have been modified to use the new -Exception parameter where applicable.
  • Spaces have been made uniform across touched files.

Daniel Belcher added 6 commits January 23, 2018 13:59
  This optional parameter will enable easier logging of exception information.

- Implement pipeline support for -Message parameter
  This parameter always accepted pipeline input but didn't follow existing PoSh paradigms
  for pipelining. Note that there is no performance benefit to pipelining, but the option is there.

- Add Write-LogHelper.
  This cmdlet is not intended to be used by itself, but instead should support Write-Log. The cmdlet
  performs the actual act of writing to screen/log.
  While it was nice to break things up, keeping the actual writing logic in Write-Log will lead
  to better exception information if the Write-* cmdlets fail, because the exception will show
  as originating from Write-Log (which makes sense), instead of Write-LogHelper (which makes less sense).
Copy link
Member

@HowardWolosky HowardWolosky left a comment

Choose a reason for hiding this comment

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

This is a whole lot of goodness...thanks.
The space normalization is appreciated too. In the future though, it would be great to have that come in as a separate change.


Logs the message:

Write-LogHelper : 2018-01-23 12:57:37 : dabelc : There was a problem.
Copy link
Member

Choose a reason for hiding this comment

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

Not Write-LogHelper anymore, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks


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
$maxPidDigits = 10 # This is an estimate (see https://stackoverflow.com/questions/17868218/what-is-the-maximum-process-id-on-windows)
Copy link
Member

Choose a reason for hiding this comment

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

This had been capitalized to signify it as a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then I'd say it failed to signify that because I thought it was using the parameter naming convention. Why not all caps? Or local variable naming convention and use Set-Variable with the Constant/ReadOnly option to show that the value shouldn't be changed?

Copy link
Member

Choose a reason for hiding this comment

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

At the time, I was unaware of the ReadOnly/Constant options for the -Option switch with Set-Variable....thanks for that. PowerShell best practices indicates PascalCase for constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Revert this change.
  2. Use Set-Variable with ReadOnly

Let me know which you'd prefer.

@@ -1142,7 +1142,7 @@ function Update-ApplicationSubmission
$output = @()
$output += "We can only modify a submission that is in the '$script:keywordPendingCommit' state."
$output += "The submission that you requested to modify ($SubmissionId) is in '$($submissionToUpdate.status)' state."

$newLineOutput = ($output -join [Environment]::NewLine)
Copy link
Member

Choose a reason for hiding this comment

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

It seems like there are probably a lot of opportunities throughout the code to now remove this type of line and just directly pass-in the array of strings, now that Write-Log will take care of NewLine'ing them for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I'll fix them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -528,14 +526,13 @@ function Show-ImageFileNames
$output += " * [$langCode]: " + ($LangImageNames.$langCode -join ", ")
}

Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

If you're here, I'd ask that you explicitly add the -Message at the same time, just to help us get up to the consistency we've been working towards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went all-in and replaced all instances of Write-Log that used implicit -Message parameter.

@@ -1058,14 +1056,13 @@ function Show-ImageFileNames
$output += " * [$langCode]: " + ($LangImageNames.$langCode -join ", ")
}

Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -1377,7 +1377,7 @@ function Start-SubmissionMonitor
$shouldMonitor = $false
}

Write-Log $($body -join [Environment]::NewLine)
Write-Log $body
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member

@HowardWolosky HowardWolosky left a comment

Choose a reason for hiding this comment

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

Only remaining thing would be to add the -Message to those simple Write-Log lines that you were touching, and then to run an internal build and validate that packaging/submission still works fine and that the logs look reasonable.

@@ -300,7 +300,7 @@ function Format-Application

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -524,7 +524,7 @@ function Format-ApplicationSubmission

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -321,7 +321,7 @@ function Format-ApplicationFlight

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -734,7 +733,7 @@ function Format-ApplicationFlightSubmission

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -296,7 +296,7 @@ function Format-InAppProduct

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -854,7 +854,7 @@ function Format-InAppProductSubmission

End
{
Write-Log $($output -join [Environment]::NewLine)
Write-Log $output
Copy link
Member

Choose a reason for hiding this comment

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

same

Daniel Belcher added 2 commits January 29, 2018 16:28
This commit replaces all (most?) instances of using Write-Log with implicit -Message parameter.
Modified lines were found with the following regex replacement:

    Find   : Write-Log\s([^-])
	Replace: Write-Log -Message $1
@danbelcher-MSFT danbelcher-MSFT merged commit 340981e into microsoft:master Jan 30, 2018
@danbelcher-MSFT danbelcher-MSFT deleted the user/dabelc/BetterExceptionLogging branch January 30, 2018 01:06
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.

None yet

2 participants