2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
Describe "Trace-Command" -tags "Feature" {
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-08-25 17:49:32 +00:00
|
|
|
Context "Listener options" {
|
2016-07-25 11:06:55 -07:00
|
|
|
BeforeAll {
|
2020-05-07 13:00:30 +01:00
|
|
|
$logFile = Setup -f traceCommandLog.txt -pass
|
|
|
|
|
$actualLogFile = Setup -f actualTraceCommandLog.txt -pass
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterEach {
|
2020-05-07 13:00:30 +01:00
|
|
|
if ( Test-Path $logfile ) { Remove-Item $logFile }
|
|
|
|
|
if ( Test-Path $actualLogFile ) { Remove-Item $actualLogFile }
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
It "LogicalOperationStack works" -Pending:($IsCoreCLR) {
|
2016-07-25 11:06:55 -07:00
|
|
|
$keyword = "Trace_Command_ListenerOption_LogicalOperationStack_Foo"
|
|
|
|
|
$stack = [System.Diagnostics.Trace]::CorrelationManager.LogicalOperationStack
|
|
|
|
|
$stack.Push($keyword)
|
|
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption LogicalOperationStack -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
|
2017-01-16 13:31:14 -08:00
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*LogicalOperationStack=$keyword*"}
|
2018-03-23 10:26:23 -07:00
|
|
|
$log.Count | Should -BeGreaterThan 0
|
2017-01-16 13:31:14 -08:00
|
|
|
}
|
2016-07-25 11:06:55 -07:00
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
It "Callstack works" -Pending:($IsCoreCLR) {
|
|
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption Callstack -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*Callstack= * System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)*"}
|
2018-03-23 10:26:23 -07:00
|
|
|
$log.Count | Should -BeGreaterThan 0
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Datetime works" {
|
|
|
|
|
$expectedDate = Trace-Command -Name * -Expression {Get-Date} -ListenerOption DateTime -FilePath $logfile
|
|
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*DateTime=*"}
|
|
|
|
|
$results = $log | ForEach-Object {[DateTime]::Parse($_.Split("=")[1])}
|
|
|
|
|
|
|
|
|
|
## allow a gap of 6 seconds. All traces should be finished within 6 seconds.
|
|
|
|
|
$allowedGap = [timespan](60 * 1000 * 1000)
|
2017-01-16 13:31:14 -08:00
|
|
|
$results | ForEach-Object {
|
|
|
|
|
$actualGap = $_ - $expectedDate;
|
|
|
|
|
if ($expectedDate -gt $_)
|
|
|
|
|
{
|
2016-07-25 11:06:55 -07:00
|
|
|
$actualGap = $expectedDate - $_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$allowedGap | Should -BeGreaterThan $actualGap
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 13:31:14 -08:00
|
|
|
It "None options has no effect" {
|
2020-05-07 13:00:30 +01:00
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption None -FilePath $actualLogfile
|
|
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
Compare-Object (Get-Content $actualLogfile) (Get-Content $logfile) | Should -BeNullOrEmpty
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "ThreadID works" {
|
2020-05-07 13:00:30 +01:00
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption ThreadId -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*ThreadID=*"}
|
|
|
|
|
$results = $log | ForEach-Object {$_.Split("=")[1]}
|
|
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$results | ForEach-Object { $_ | Should -Be ([threading.thread]::CurrentThread.ManagedThreadId) }
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Timestamp creates logs in ascending order" {
|
2020-05-07 13:00:30 +01:00
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption Timestamp -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*Timestamp=*"}
|
|
|
|
|
$results = $log | ForEach-Object {$_.Split("=")[1]}
|
|
|
|
|
$sortedResults = $results | Sort-Object
|
2018-03-23 10:26:23 -07:00
|
|
|
$sortedResults | Should -Be $results
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "ProcessId logs current process Id" {
|
2020-05-07 13:00:30 +01:00
|
|
|
Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption ProcessId -FilePath $logfile
|
2016-07-25 11:06:55 -07:00
|
|
|
$log = Get-Content $logfile | Where-Object {$_ -like "*ProcessID=*"}
|
|
|
|
|
$results = $log | ForEach-Object {$_.Split("=")[1]}
|
|
|
|
|
|
2020-01-13 19:34:38 +00:00
|
|
|
$results | ForEach-Object { $_ | Should -Be $PID }
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
}
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|