Comments (6)
GetConsumingEnumerable
The count does not drop to zero just by calling 'GetConsumingEnumerable()'.
This can be shown by the following code snippet.
New-Variable -Name LoggingEventQueue -Scope Script -Value ([System.Collections.Concurrent.BlockingCollection[int]]::new(1000))
New-Variable -Name LoggingRunspace -Scope Script -Option ReadOnly -Value ([hashtable]::Synchronized(@{ }))
$Script:InitialSessionState = [initialsessionstate]::CreateDefault()
if ($Script:InitialSessionState.psobject.Properties['ApartmentState']) {
$Script:InitialSessionState.ApartmentState = [System.Threading.ApartmentState]::MTA
}
# Importing variables into runspace
foreach ($sessionVariable in 'LoggingEventQueue') {
$Value = Get-Variable -Name $sessionVariable -ErrorAction Continue -ValueOnly
Write-Verbose "Importing variable $sessionVariable`: $Value into runspace"
$v = New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList $sessionVariable, $Value, '', ([System.Management.Automation.ScopedItemOptions]::AllScope)
$Script:InitialSessionState.Variables.Add($v)
}
#Setup runspace
$Script:LoggingRunspace.Runspace = [runspacefactory]::CreateRunspace($Script:InitialSessionState)
$Script:LoggingRunspace.Runspace.Name = 'LoggingQueueConsumer'
$Script:LoggingRunspace.Runspace.Open()
# Spawn Logging Consumer
$Script:LoggingEventQueue.Add(1)
$Script:LoggingEventQueue.Add(2)
$Script:LoggingEventQueue.Add(3)
$Script:LoggingEventQueue.Add(4)
$Script:LoggingEventQueue.Add(5)
$Script:LoggingEventQueue.Add(6)
$Script:LoggingEventQueue.Add(7)
$Script:LoggingEventQueue.Add(8)
$Script:LoggingEventQueue.Add(9)
$Script:LoggingEventQueue.Add(10)
$Consumer = {
foreach ($Log in $Script:LoggingEventQueue.GetConsumingEnumerable()) {
Start-Sleep -Milliseconds 200
}
}
$Script:LoggingRunspace.Powershell = [Powershell]::Create().AddScript($Consumer, $true)
$Script:LoggingRunspace.Powershell.Runspace = $Script:LoggingRunspace.Runspace
$Script:LoggingRunspace.Handle = $Script:LoggingRunspace.Powershell.BeginInvoke()
while ($Script:LoggingEventQueue.Count -gt 0){
Write-Host $Script:LoggingEventQueue.Count
}
Write-Host "Fin"
Wait-Logging
The last part is about messages being lost, the only portion that should (now) be able to be lost is the last message with Wait-Logging. This could be "fixable" by simply adding another Wait-Sleep after the loop.
(I used my current feature implementation, however this should not alter from the current master.)
function Config-Logging {
param(
[Parameter(Mandatory)]
[String]
$Name
)
Set-LoggingDefaultLevel -Level 'INFO'
Add-LoggingTarget -Name File -Configuration @{Path = "C:\Users\soennecken\source\repos\private\Logging\ParallelTest\Run_"+$Name+".txt"}
}
$Level = 'DEBUG', 'INFO', 'WARNING', 'ERROR'
$LoggingDemo = {
Config-Logging -Name ([Guid]::NewGuid())
foreach ($i in 1..1000) {
Write-Log -Message 'Message n. {0} , Job{1} ' -Arguments @($i, $_) -Level ERROR
Start-Sleep -Milliseconds (Get-Random -Min 2 -Max 10)
}
Wait-Logging
}
1..5 | Start-RSJob -ScriptBlock $LoggingDemo -ModulesToImport 'Logging', 'PoshRSJob' -VariablesToImport 'Level' -FunctionsToImport 'Config-Logging' | Wait-RSJob | Receive-RSJob
from logging.
I need to take a look into this issue cause my module uses runspace to spawn a thread that actually write the log.
Maybe a runspace inside another runspace is not a good idea for the sanity of the environment in which it runs.
I'll take a look to see if I can work around something
from logging.
Hi,
I also notice that some message appears after Wait-Logging and can be lost at the end of a script.
I am using those two target: WinEventLog & Console.
I am having a real hard time to understand the mechanic behind the module as I am not familiar with anything related to “runspace”.
I notice that Wait-Logging is waiting for “LoggingEventQueue.Count -gt 0”. However, is the count goes to Zero as soon as “.GetConsumingEnumerable()” is executed?
As a workaround, I move the sleep in Wait-Logging to be after the Count check. And that does help. However, I need at least 200ms to be somewhat reliable.
from logging.
@SilentBob999 Are you using the logging module with PoshRSJob?
from logging.
Hi @EsOsO ,
I do not use PoshRSJob.
It is all sequential, no "jobs" of any kind. Just a lot of code fragmentation (dot sourcing, import-module, etc....)
from logging.
@EsOsO I think this issue can now be closed.
from logging.
Related Issues (20)
- AzureLogAnalytics target generates an error when no Body provided
- Create dev builds
- Build against Linux, MacOS, Windows / Windows Powershell, Powershell
- Are target names case sensitive? HOT 2
- Add variable hostname to $logMessage HOT 6
- Support for multiple Teams channels HOT 2
- File Target is converting $Log.body to json permanently HOT 1
- Not support Chinese? or any other Unicode language?
- Import-Module raises errors if in a script strict mode
- MS Teams Target: Proxy Support HOT 1
- Get-LoggingMessageCount.ps1 missing
- Wait-Logging Hanging Scheduled Task HOT 3
- Logging target mail: send Bulk Log
- Add support for PS transcript (Start-Transcript)
- Question: Support for colors in the format string
- Reset-LoggingTarget or -Scope param? HOT 6
- File Target with Relative Path HOT 4
- Most targets ignore -ExceptionInfo HOT 1
- SEQ: structured logging not implemented HOT 4
- NotifyBeginApplication/NotifyEndApplication calls not needed?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logging.