functions/logging/Set-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
function Set-PSFLoggingProvider
{
<#
 .SYNOPSIS
  Configures a logging provider.
  
 .DESCRIPTION
  This command allows configuring the way a logging provider works.
  This grants the ability to ...
  - Enable / Disable a provider
  - Set additional parameters defined by the provider (each provider may implement its own settings, exposed through dynamic parameters)
  - Configure filters about what messages get sent to a given provider.
  
 .PARAMETER Name
  The name of the provider to configure
  
 .PARAMETER Enabled
  Whether the provider should be enabled or disabled.
  
 .PARAMETER IncludeModules
  Only messages from modules listed here will be logged.
  Exact match only, an empty list results in all modules being logged.
  
 .PARAMETER ExcludeModules
  Messages from excluded modules will not be logged using this provider.
  Overrides -IncludeModules in case of overlap.
  
 .PARAMETER IncludeTags
  Only messages containing the listed tags will be logged.
  Exact match only, only a single match is required for a message to qualify.
  
 .PARAMETER ExcludeTags
  Messages containing any of the listed tags will not be logged.
  Overrides -IncludeTags in case of overlap.
  
 .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
  PS C:\> Set-PSFLoggingProvider -Name filesystem -Enabled $false
   
  Disables the filesystem provider.
  
 .EXAMPLE
  PS C:\> Set-PSFLoggingProvider -Name filesystem -ExcludeModules "PSFramework"
  
  Prevents all messages from the PSFramework module to be logged to the file system
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [Alias('Provider', 'ProviderName')]
        [string]
        $Name,
        
        [bool]
        $Enabled,
        
        [string[]]
        $IncludeModules,
        
        [string[]]
        $ExcludeModules,
        
        [string[]]
        $IncludeTags,
        
        [string[]]
        $ExcludeTags,
        
        [switch]
        $EnableException
    )
    
    dynamicparam
    {
        if ($Name -and ([PSFramework.Logging.ProviderHost]::Providers.ContainsKey($Name.ToLower())))
        {
            [scriptblock]::Create(([PSFramework.Logging.ProviderHost]::Providers[$Name.ToLower()].ConfigurationParameters)).Invoke()
        }
    }
    
    begin
    {
        if (-not ([PSFramework.Logging.ProviderHost]::Providers.ContainsKey($Name.ToLower())))
        {
            Stop-PSFFunction -Message "Provider $Name not found!" -EnableException $EnableException -Category InvalidArgument -Target $Name
            return
        }
        
        [PSFramework.Logging.Provider]$provider = [PSFramework.Logging.ProviderHost]::Providers[$Name.ToLower()]
        
        if ((-not $provider.Enabled) -and (-not ([scriptblock]::Create($provider.IsInstalledScript).Invoke())) -and $Enabled)
        {
            Stop-PSFFunction -Message "Provider $Name not installed! Run 'Install-PSFLoggingProvider' first" -EnableException $EnableException -Category InvalidOperation -Target $Name
            return
        }
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        # Recreating the scriptblock this way ensures that it can properly inherit the variables in the current scope
        [System.Management.Automation.ScriptBlock]::Create($provider.ConfigurationScript).Invoke()
        
        #region Filter Configuration
        if (Test-PSFParameterBinding -ParameterName "IncludeModules")
        {
            $provider.IncludeModules = $IncludeModules
            Set-PSFConfig -FullName "LoggingProvider.$($provider.Name).IncludeModules" -Value $IncludeModules
        }
        if (Test-PSFParameterBinding -ParameterName "ExcludeModules")
        {
            $provider.ExcludeModules = $ExcludeModules
            Set-PSFConfig -FullName "LoggingProvider.$($provider.Name).ExcludeModules" -Value $ExcludeModules
        }
        
        if (Test-PSFParameterBinding -ParameterName "IncludeTags")
        {
            $provider.IncludeTags = $IncludeTags
            Set-PSFConfig -FullName "LoggingProvider.$($provider.Name).IncludeTags" -Value $IncludeTags
        }
        if (Test-PSFParameterBinding -ParameterName "ExcludeTags")
        {
            $provider.ExcludeTags = $ExcludeTags
            Set-PSFConfig -FullName "LoggingProvider.$($provider.Name).ExcludeTags" -Value $ExcludeTags
        }
        #endregion Filter Configuration
        
        if (Test-PSFParameterBinding -ParameterName "Enabled")
        {
            $provider.Enabled = $Enabled
            Set-PSFConfig -FullName "LoggingProvider.$($provider.Name).Enabled" -Value $Enabled
        }
    }
    end
    {
        if (Test-PSFFunctionInterrupt) { return }
    }
}