0

I have an PowerShell module that initializes a log4net with a default config file so I can use this in my PS Scripts. This works fine. My module has a parameter so i can specify to with file the RollingFileAppender should write. My module updates this value (when debugging, I can see that the File-property is changed). But when returning the 'logger' object, it still logs to the file that configured in my default config file.

This used to work some time ago, but now it does not any more. Basically, this is the code of the module (I removed all the try-catch code for clarity).

This is my module 'log4net.psm1'

function InitLog4Net { param( [string]$Logfile, [string]$FileThreshold ) $ErrorActionPreference = "Stop" [void][Reflection.Assembly]::LoadFile("$PSScriptRoot\log4net.dll"); $fileInfo = New-Object System.IO.FileInfo("$PSScriptRoot\log4net.config") [log4net.Config.XmlConfigurator]::Configure($fileInfo) $log = [log4net.LogManager]::GetLogger("Logger") if ($PSBoundParameters.ContainsKey("Logfile")) { foreach ($appender in $log.Logger.Appenders) { if ($appender.Name -eq "file") { $appender.File = $Logfile $appender.ActivateOptions() } } } # I can see the $appender.File is updated here return $log } 

I simply call from a PS Script this module that return the 'log4net' object and I start logging. The log is written to the file that is defined in my default log4net.config.

Import-Module c:\<path-to-module>\log4net.psm1 -Force $file = $PSScriptRoot + "\Do-Monitor.log" $log = InitLog4Net -Logfile $file $log.Info("Started!") 

What am i doing wrong here? Am i returning the original object without my modification?

Greetz Pieter

I'm using Powershell 7.4.6 on a Windows Server 2019, log4net.dll is version 2.0.15.0

UPDATE: just rebooted the server and it worked perfectly. We run several Powershell scripts on a daily basis on this server. After the reboot, my script was the first to run. I started another PowerShell script that is using the same module I created and then it was broke again. So it goes wrong when running multiple powershell scripts that are using my module.

5
  • How is the module loaded and how is it used? Is it a real .psm1 module that is imported using Import-Module or a .ps1 file that is called or dot-sourced? Could you provide a minimal reproducible example? Commented Jan 28 at 12:00
  • 1
    OK, edited the original post Commented Jan 28 at 12:56
  • 1
    Try to remove the -Force parameter from the Import-Module calls, so all scripts will use the same instance of the module and thus share the module state. Move the common log4net initialization code out of the function, so it runs only once when the module is imported the first time. While loading .NET assemblies manually can work, the module manifest has a specific section RequiredAssemblies for declaring assembly dependencies, which I would use instead of [Reflection.Assembly]::LoadFile. Commented Jan 28 at 20:11
  • For now, its working when I remove the -Force parameter from the Import-Module. I added this for initial testing when the module is changed a lot and i test this using the same powershell instance. I'm not sure i understand the need to move the log4net initialization out of the function. I call this function only once after importing the module, the ps scripts do their thing and exit. Meanwhile, i will look at that RequiredAssemblies method (i'm unfamiliar with this command). Commented Jan 29 at 7:39
  • RequiredAssemblies isn't a command. It is a key in the module's manifest (.psd1) file. See learn.microsoft.com/en-us/powershell/scripting/developer/module/… Commented Jan 29 at 10:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.