xBarns
xBarns

Reputation: 291

LogManager.GetLogger in Powershell Module returns multiple objects

I am trying to wrap the nlog.dll in a module so i can reuse that mechanism for multiple scripts.

In the module I do this:

Set-StrictMode -Version Latest

function Get-NewLoggerTest() {
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [Parameter(Mandatory=$true,HelpMessage="Provide the name of the logger.")]
        [string]$loggerName
    )

    [Reflection.Assembly]::LoadFile("C:\Program Files\WindowsPowerShell\Modules\ToolsTest\nlog.dll")

    # Creater logger object
    $logger = [NLog.LogManager]::GetLogger($loggerName)

    return $logger
}

Export-ModuleMember -Function Get-NewLoggerTest

Now I create a new logger by issuing:

Import-Module ToolsTest -Force
# Create a new logger
$logNew = Get-NewLoggerTest -loggerName "TEST Logger"

The problem i now have is that i have to reference the logging object by using

$logNew[1].Debug("Debug Message")

$logNew.Debug returns an error because somehow multiple objects are returned.

If I do a

$newLog | gm

it returns multiple type names:

TypeName: System.Reflection.RuntimeAssembly
TypeName: NLog.Logger

I only care for the NLog one, any ideas how i can get rid of the other one?

Of course I can reassign it like

$logNew = $logNew[1]

but that's an extra step I want to avoid.

Upvotes: 1

Views: 310

Answers (1)

xBarns
xBarns

Reputation: 291

I think i found the solution.

Moving

[Reflection.Assembly]::LoadFile("C:\Program Files\WindowsPowerShell\Modules\ToolsTest\nlog.dll")

outside the function, solved the problem. I am not sure why, but it works now.

Upvotes: 1

Related Questions