functions/logging/Register-PSFLoggingProvider.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
function Register-PSFLoggingProvider
{
    <#
  .SYNOPSIS
   Registers a new logging provider to the PSFramework logging system.
   
  .DESCRIPTION
   This function registers all components of the PSFramework logging provider systems.
   It allows you to define your own logging destination and configuration and tie them into the default logging system.
    
   In order to properly utilize its power, it becomes necessary to understand how the logging works beneath the covers:
   - On Start of the logging script, it runs a one-time scriptblock per enabled provider (this will also occur when later enabling a provider)
   - Thereafter the script will continue, logging in cycles of Start > Log all Messages > Log all Errors > End
   Each of those steps has its own event, allowing for fine control over what happens where.
   - Finally, on shutdown of a provider it again offers an option to execute some code (to dispose/free resources in use)
    
   All providers share the same scope for the execution of ALL of those actions/scriptblocks!
   This makes it important to give your variables/functions a unique name, in order to avoid conflicts.
   General Guideline:
   - All variables should start with the name of the provider and an underscore. Example: $filesystem_root
   - All functions should use the name of the provider as prefix. Example: Clean-FileSystemErrorXml
    
   A simple implementation example can be seen with the FileSystem provider, stored in the module folder:
   internal/loggingProvider/filesystem.provider.ps1
   
  .PARAMETER Name
   A unique name for your provider. Registering a provider under a name already registered, NOTHING will happen.
   This function will instead silently terminate.
   
  .PARAMETER Enabled
   Setting this will enable the provider on registration.
   
  .PARAMETER RegistrationEvent
   Scriptblock that should be executed on registration.
   This allows you to perform installation actions synchroneously, with direct user interaction.
   At the same time, by adding it as this parameter, it will only performed on the initial registration, rather than every time the provider is registered (runspaces, Remove-Module/Import-Module)
   
  .PARAMETER BeginEvent
   The actions that should be taken once when setting up the logging.
   Can well be used to register helper functions or loading other resources that should be loaded on start only.
   
  .PARAMETER StartEvent
   The actions taken at the beginning of each logging cycle.
   Typically used to establish connections or do some necessary pre-connections.
   
  .PARAMETER MessageEvent
   The actions taken to process individual messages.
   The very act of writting logs.
   This scriptblock receives a message object (As returned by Get-PSFMessage) as first and only argument.
   Under some circumstances, this message may be a $null object, your scriptblock must be able to handle this.
   
  .PARAMETER ErrorEvent
   The actions taken to process individual error messages.
   The very act of writting logs.
   This scriptblock receives a message object (As returned by 'Get-PSFMessage -Errors') as first and only argument.
   Under some circumstances, this message may be a $null object, your scriptblock must be able to handle this.
   This consists of complex, structured data and may not be suitable to all logging formats.
   However all errors are ALWAYS accompanied by a message, making integrating this optional.
   
  .PARAMETER EndEvent
   Actions taken when finishing up a logging cycle. Can be used to close connections.
   
  .PARAMETER FinalEvent
   Final action to take when the logging terminates.
   This should release all resources reserved.
   This event will fire when:
   - The console is being closed
   - The logging script is stopped / killed
   - The logging provider is disabled
   
  .PARAMETER ConfigurationParameters
   The function Set-PSFLoggingProvider can be used to configure this logging provider.
   Using this parameter it is possible to register dynamic parameters when configuring your provider.
   
  .PARAMETER ConfigurationScript
   When using Set-PSFLoggingProvider, this script can be used to input given by the dynamic parameters generated by the -ConfigurationParameters parameter.
   
  .PARAMETER IsInstalledScript
   A scriptblock verifying that all prerequisites are properly installed.
   
  .PARAMETER InstallationScript
   A scriptblock performing the installation of the provider's prerequisites.
   Used by Install-PSFProvider in conjunction with the script provided by -InstallationParameters
   
  .PARAMETER InstallationParameters
   A scriptblock returning dynamic parameters that are offered when running Install-PSFprovider.
   Those can then be used by the installation scriptblock specified in the aptly named '-InstallationScript' parameter.
   
  .PARAMETER ConfigurationSettings
   This is executed before actually registering the scriptblock.
   It allows you to include any logic you wish, but it is specifically designed for configuration settings using Set-PSFConfig with the '-Initialize' parameter.
  
  .PARAMETER EnableException
   This parameters disables user-friendly warnings and enables the throwing of exceptions.
   This is less user friendly, but allows catching exceptions in calling scripts.
  
  .EXAMPLE
   Register-PSFLoggingProvider -Name "filesystem" -Enabled $true -RegistrationEvent $registrationEvent -BeginEvent $begin_event -StartEvent $start_event -MessageEvent $message_Event -ErrorEvent $error_Event -EndEvent $end_event -FinalEvent $final_event -ConfigurationParameters $configurationParameters -ConfigurationScript $configurationScript -IsInstalledScript $isInstalledScript -InstallationScript $installationScript -InstallationParameters $installationParameters -ConfigurationSettings $configuration_Settings
  
   Registers the filesystem provider, providing events for every single occasion.
#>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string]
        $Name,
        
        [switch]
        $Enabled,
        
        [System.Management.Automation.ScriptBlock]
        $RegistrationEvent,
        
        [System.Management.Automation.ScriptBlock]
        $BeginEvent = { },
        
        [System.Management.Automation.ScriptBlock]
        $StartEvent = { },
        
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.ScriptBlock]
        $MessageEvent,
        
        [System.Management.Automation.ScriptBlock]
        $ErrorEvent = { },
        
        [System.Management.Automation.ScriptBlock]
        $EndEvent = { },
        
        [System.Management.Automation.ScriptBlock]
        $FinalEvent = { },
        
        [System.Management.Automation.ScriptBlock]
        $ConfigurationParameters = { },
        
        [System.Management.Automation.ScriptBlock]
        $ConfigurationScript = { },
        
        [System.Management.Automation.ScriptBlock]
        $IsInstalledScript = { $true },
        
        [System.Management.Automation.ScriptBlock]
        $InstallationScript = { },
        
        [System.Management.Automation.ScriptBlock]
        $InstallationParameters = { },
        
        [System.Management.Automation.ScriptBlock]
        $ConfigurationSettings,
        
        [switch]
        $EnableException
    )
    
    if ([PSFramework.Logging.ProviderHost]::Providers.ContainsKey($Name.ToLower()))
    {
        return
    }
    
    if ($ConfigurationSettings) { . $ConfigurationSettings }
    if (Test-PSFParameterBinding -ParameterName Enabled)
    {
        Set-PSFConfig -FullName "LoggingProvider.$Name.Enabled" -Value $Enabled.ToBool()
    }
    
    $provider = New-Object PSFramework.Logging.Provider
    $provider.Name = $Name
    $provider.BeginEvent = $BeginEvent
    $provider.StartEvent = $StartEvent
    $provider.MessageEvent = $MessageEvent
    $provider.ErrorEvent = $ErrorEvent
    $provider.EndEvent = $EndEvent
    $provider.FinalEvent = $FinalEvent
    $provider.ConfigurationParameters = $ConfigurationParameters
    $provider.ConfigurationScript = $ConfigurationScript
    $provider.IsInstalledScript = $IsInstalledScript
    $provider.InstallationScript = $InstallationScript
    $provider.InstallationParameters = $InstallationParameters
    
    $provider.IncludeModules = Get-PSFConfigValue -FullName "LoggingProvider.$Name.IncludeModules" -Fallback @()
    $provider.ExcludeModules = Get-PSFConfigValue -FullName "LoggingProvider.$Name.ExcludeModules" -Fallback @()
    $provider.IncludeTags = Get-PSFConfigValue -FullName "LoggingProvider.$Name.IncludeTags" -Fallback @()
    $provider.ExcludeTags = Get-PSFConfigValue -FullName "LoggingProvider.$Name.ExcludeTags" -Fallback @()
    
    $provider.InstallationOptional = Get-PSFConfigValue -FullName "LoggingProvider.$Name.InstallOptional" -Fallback $false
    
    [PSFramework.Logging.ProviderHost]::Providers[$Name.ToLower()] = $provider
    
    try { if ($RegistrationEvent) { . $RegistrationEvent } }
    catch
    {
        [PSFramework.Logging.ProviderHost]::Providers.Remove($Name.ToLower())
        Stop-PSFFunction -Message "Failed to register logging provider '$Name' - Registration event failed." -ErrorRecord $_ -EnableException $EnableException -Tag 'logging', 'provider', 'fail', 'register'
        return
    }
    
    $shouldEnable = Get-PSFConfigValue -FullName "LoggingProvider.$Name.Enabled" -Fallback $false
    $isInstalled = [System.Management.Automation.ScriptBlock]::Create($provider.IsInstalledScript).Invoke()
    
    if (-not $isInstalled -and (Get-PSFConfigValue -FullName "LoggingProvider.$Name.AutoInstall" -Fallback $false))
    {
        try { Install-PSFLoggingProvider -Name $Name -EnableException }
        catch
        {
            if ($provider.InstallationOptional)
            {
                Write-PSFMessage -Level Warning -Message "Failed to install logging provider '$Name'" -ErrorRecord $_ -Tag 'logging', 'provider', 'fail', 'install' -EnableException $EnableException
            }
            else
            {
                Stop-PSFFunction -Message "Failed to install logging provider '$Name'" -ErrorRecord $_ -EnableException $EnableException -Tag 'logging', 'provider', 'fail', 'install'
                return
            }
        }
    }
    
    if ($shouldEnable)
    {
        if ($isInstalled) { $provider.Enabled = $true }
        else
        {
            Stop-PSFFunction -Message "Failed to enable logging provider $Name on registration! It was not recognized as installed. Consider running 'Install-PSFProvider' to properly install the prerequisites." -ErrorRecord $_ -EnableException $EnableException -Tag 'logging', 'provider', 'fail', 'install'
            return
        }
    }
}