Skip to content

Commit 0062b41

Browse files
committed
2 parents 9d07f04 + 540d4b0 commit 0062b41

7 files changed

Lines changed: 54 additions & 4 deletions

File tree

270 KB
Binary file not shown.
File renamed without changes.

Public/Debugging/initialize-log4net.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,33 @@ function Initialize-Log4Net() {
5252
# Execution #
5353
#############
5454
Begin {
55-
# Import the log4net assembly
56-
Add-Type -Path $PSScriptRoot/../../Artefacts/log4net/Assemblies/bin/log4net.dll
55+
[Bool]$IsPSCore = Test-IsPSCore
56+
if ($IsPSCore) {
57+
# Import the log4net assembly - supporting PS Core.
58+
Add-Type -Path $PSScriptRoot/../../Artefacts/log4net/bin/netstandard/log4net.dll
59+
} else {
60+
# Import the log4net assembly - supporting .NET Framework 4.5 (e.g. Windows)
61+
Add-Type -Path $PSScriptRoot/../../Artefacts/log4net/bin/net/log4net.dll
62+
}
63+
64+
# Compensate for non-trailing slash on the LogFilesPath value.
65+
if (-not $LogFilesPath.EndsWith("/")) {
66+
$LogFilesPath = "$LogFilesPath/"
67+
}
5768
}
5869
Process {
5970
# Global setting, the log filename and the path to it. Combined.
6071
[log4net.GlobalContext]::Properties["LogFileName"] = "$LogFilesPath$logFileName.log"
6172

6273
# Set up the repository and the XmlConfigurator. In configure and watch mode. Meaning that the config file can be changed and the changes will be hot-loaded.
63-
$logRepository = [log4net.LogManager]::GetRepository([System.Reflection.Assembly]::GetEntryAssembly())
74+
$EntryAssembly = [System.Reflection.Assembly]::GetEntryAssembly()
75+
if ($null -ne $EntryAssembly) {
76+
# Calling [System.Reflection.Assembly]::GetEntryAssembly() worked. The log4net assembly was retrieved.
77+
$logRepository = [log4net.LogManager]::GetRepository($EntryAssembly)
78+
} else {
79+
# As using > [System.Reflection.Assembly]::GetEntryAssembly() can return null in certain cases. Try with a fallback method to retrieve the loaded log4net assembly.
80+
$logRepository = [log4net.LogManager]::GetRepository(([appdomain]::currentdomain.getassemblies() | Where-Object { $_.Location -match "log4net" }))
81+
}
6482
[log4net.Config.XmlConfigurator]::ConfigureAndWatch($logRepository,(Get-Item $log4NetConfigFile))
6583

6684
# Instantiate a log4Net logger that will base its settings on the config in the provided .xml file
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function Test-IsPSCore() {
2+
<#
3+
.DESCRIPTION
4+
Tests whether the PowerShell runtime environment is PS Core.
5+
.INPUTS
6+
<none>
7+
.OUTPUTS
8+
[Bool] representing the result of testing whether the PowerShell runtime environment is PS Core.
9+
.NOTES
10+
<none>
11+
.EXAMPLE
12+
PS C:\> [Bool]$Result = Test-IsPSCore
13+
Invokes Test-IsPSCore to determine whether the PowerShell runtime environment is PS Core.
14+
#>
15+
16+
# Define parameters
17+
[CmdletBinding()]
18+
[OutputType([Bool])]
19+
param()
20+
21+
#############
22+
# Execution #
23+
#############
24+
Begin {}
25+
Process {
26+
[Bool]$Result = $PSVersionTable.PSEdition -ieq 'core'
27+
}
28+
End {
29+
# Return the
30+
$Result
31+
}
32+
}

powershellTooling.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
GUID = 'c1e187cd-222f-4df6-8238-88f90f0284ee'
88

99
# Version number of this module.
10-
ModuleVersion = '0.0.15'
10+
ModuleVersion = '0.0.16'
1111

1212
# Author of this module
1313
Author = 'Lars S. Bengtsson | https://github.com/larssb | https://bengtssondd.it'

0 commit comments

Comments
 (0)