internal/scripts/async-logging2.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
$scriptBlock = {
    try
    {
        $script:___ScriptName = 'PSFramework.Logging'
        
        while ($true)
        {
            # This portion is critical to gracefully closing the script
            if ([PSFramework.Runspace.RunspaceHost]::Runspaces[$___ScriptName.ToLower()].State -notlike "Running")
            {
                break
            }
            
            #region Manage Begin Event
            foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetEnabled())
            {
                if (-not $___provider.Initialized)
                {
                    try
                    {
                        $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.BeginEvent)), $null, $null)
                        $___provider.Initialized = $true
                    }
                    catch { $___provider.Errors.Push($_) }
                }
            }
            #endregion Manage Begin Event
            
            #region Start Event
            foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
            {
                try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.StartEvent)), $null, $null) }
                catch { $___provider.Errors.Push($_) }
            }
            #endregion Start Event
            
            #region Message Event
            while ([PSFramework.Message.LogHost]::OutQueueLog.Count -gt 0)
            {
                $Entry = $null
                [PSFramework.Message.LogHost]::OutQueueLog.TryDequeue([ref]$Entry)
                if ($Entry)
                {
                    foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
                    {
                        if ($___provider.MessageApplies($Entry))
                        {
                            try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.MessageEvent)), $null, $Entry) }
                            catch { $___provider.Errors.Push($_) }
                        }
                    }
                }
            }
            #endregion Message Event
            
            #region Error Event
            while ([PSFramework.Message.LogHost]::OutQueueError.Count -gt 0)
            {
                $Record = $null
                [PSFramework.Message.LogHost]::OutQueueError.TryDequeue([ref]$Record)
                
                if ($Record)
                {
                    foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
                    {
                        if ($___provider.MessageApplies($Record))
                        {
                            try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.ErrorEvent)), $null, $Record) }
                            catch { $___provider.Errors.Push($_) }
                        }
                    }
                }
            }
            #endregion Error Event
            
            #region End Event
            foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
            {
                try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.EndEvent)), $null, $null) }
                catch { $___provider.Errors.Push($_) }
            }
            #endregion End Event
            
            Start-Sleep -Seconds 1
        }
    }
    catch
    {
        
    }
    finally
    {
        #region Flush log on exit
        if (([PSFramework.Runspace.RunspaceHost]::Runspaces[$___ScriptName.ToLower()].State -like "Running") -and ([PSFramework.Configuration.ConfigurationHost]::Configurations["psframework.logging.disablelogflush"].Value))
        {
            #region Start Event
            foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
            {
                try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.StartEvent)), $null, $null) }
                catch { $___provider.Errors.Push($_) }
            }
            #endregion Start Event
            
            #region Message Event
            while ([PSFramework.Message.LogHost]::OutQueueLog.Count -gt 0)
            {
                $Entry = $null
                [PSFramework.Message.LogHost]::OutQueueLog.TryDequeue([ref]$Entry)
                if ($Entry)
                {
                    foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
                    {
                        if ($___provider.MessageApplies($Entry))
                        {
                            try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.MessageEvent)), $null, $Entry) }
                            catch { $___provider.Errors.Push($_) }
                        }
                    }
                }
            }
            #endregion Message Event
            
            #region Error Event
            while ([PSFramework.Message.LogHost]::OutQueueError.Count -gt 0)
            {
                $Record = $null
                [PSFramework.Message.LogHost]::OutQueueError.TryDequeue([ref]$Record)
                
                if ($Record)
                {
                    foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
                    {
                        if ($___provider.MessageApplies($Record))
                        {
                            try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.MessageEvent)), $null, $Record) }
                            catch { $___provider.Errors.Push($_) }
                        }
                    }
                }
            }
            #endregion Error Event
            
            #region End Event
            foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
            {
                try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.EndEvent)), $null, $null) }
                catch { $___provider.Errors.Push($_) }
            }
            #endregion End Event
        }
        #endregion Flush log on exit
        
        #region Final Event
        foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
        {
            try { $ExecutionContext.InvokeCommand.InvokeScript($false, ([System.Management.Automation.ScriptBlock]::Create($___provider.FinalEvent)), $null, $null) }
            catch { $___provider.Errors.Push($_) }
        }
        
        foreach ($___provider in [PSFramework.Logging.ProviderHost]::GetInitialized())
        {
            $___provider.Initialized = $false
        }
        #endregion Final Event
        
        [PSFramework.Runspace.RunspaceHost]::Runspaces[$___ScriptName.ToLower()].SignalStopped()
    }
}

Register-PSFRunspace -ScriptBlock $scriptBlock -Name 'PSFramework.Logging' -NoMessage
Start-PSFRunspace -Name 'PSFramework.Logging' -NoMessage