Trying to get log4net to work with PowerShell (with the log4net configuration file in the mix)

I struggled to get log4net to work with PowerShell. I have the following PowerShell code that pulls out log4net with a configuration file and then tries to make a simple entry in the log file, but what errors are missing.

Clear-History Clear-Host # Write-Host "BEGIN: Importing module psm_logger.psm1" Import-Module "C:\Users\Administrator\Desktop\ps\IIS\psm_logger.psm1" -Force Write-Host "BEGIN: log4net configuration definition" $log = New-Logger -Configuration "C:\Users\Administrator\Desktop\ps\IIS\logging\log4net.config" -Dll "C:\Users\Administrator\Desktop\ps\IIS\logging\log4net.dll" Write-Host "END: log4net configuration definition" Write-Host "BEGIN: log4net configuration DEBUG definition" #$log.DebugFormat("Logger configuration file is : '{0}'", (Resolve-Path "C:\Users\Administrator\Desktop\ps\IIS\logging\log4net.config")) Write-Host "END: log4net DEBUG configuration definition" #====================== $ThisHostname = $env:COMPUTERNAME #====================== Write-Host "BEGIN: Module/Snapin import/addition" Write-Host "" $log.Info("BEGIN: Module/Snapin import/addition") #====================== 

The above psm_logger.psm1 file contains the file:

 function New-Logger { #Write-Host "BEGIN: New-Logger" <# .SYNOPSIS This function creates a log4net logger instance already configured .OUTPUTS The log4net logger instance ready to be used #> [CmdletBinding()] Param ( [string] # Path of the configuration file of log4net $Configuration, [Alias("Dll")] [string] # Log4net dll path $log4netDllPath ) Write-Verbose "[New-Logger] Logger initialization" $log4netDllPath = Resolve-Path $log4netDllPath -ErrorAction SilentlyContinue -ErrorVariable Err if ($Err) { throw "Log4net library cannot be found on the path $log4netDllPath" } else { Write-Verbose "[New-Logger] Log4net dll path is : '$log4netDllPath'" [void][Reflection.Assembly]::LoadFrom($log4netDllPath) | Out-Null # Log4net configuration loading $log4netConfigFilePath = Resolve-Path $Configuration -ErrorAction SilentlyContinue -ErrorVariable Err if ($Err) { throw "Log4Net configuration file $Configuration cannot be found" } else { Write-Verbose "[New-Logger] Log4net configuration file is '$log4netConfigFilePath' " $FileInfo = New-Object System.IO.FileInfo($log4netConfigFilePath) [log4net.Config.XmlConfigurator]::Configure($FileInfo) $script:MyCommonLogger = [log4net.LogManager]::GetLogger("root") Write-Verbose "[New-Logger] Logger is configured" return $MyCommonLogger } } } 

The configuration file for log4net looks like this:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" **requirePermission="false"**/> </configSections> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="C:\Users\Administrator\Desktop\ps\IIS\LOGS\LogTest2.txt" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="Header" value="[Header]\r\n" /> <param name="Footer" value="[Footer]\r\n" /> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="LogFileAppender" /> </root> </log4net> </configuration> 

When starting the output AND the error received:

BEGIN: Import module psm_logger.psm1 BEGIN: define log4net configuration END: define log4net configuration START: log4net configuration DEBUG definition END: define log4net configuration DEBUG BEGIN: Import / add module / Snapin

Method call failed because [System.Object []] does not contain a method named "Information". In C: \ Users \ Administrator \ Desktop \ ps \ IIS \ IIS_localLogs.ps1: 18 char: 10 + $ log.Info <<<("START: import / add module / Snapin") + CategoryInfo: InvalidOperation: (Information: String) [], RuntimeException + FullyQualifiedErrorId: MethodNotFound

Any help would be greatly appreciated!

(FYI: I originally got this code from the best practices blog blog here: http://blog.octo.com/en/powershell-v2-my-best-practices/ , which I found very useful)

+4
source share
2 answers

Found a solution ...

The configuration file was corrupted by the presence of some stars to the top. Removing these fixed. Thanks to everyone who may have started looking at this, and I hope this helps someone else in the future.

Now the configuration file is as follows:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/> </configSections> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="C:\Users\Administrator\Desktop\ps\IIS\LOGS\LogTest2.txt" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="Header" value="[Header]\r\n" /> <param name="Footer" value="[Footer]\r\n" /> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="LogFileAppender" /> </root> </log4net> </configuration> 
+2
source

New-Logger is such a useful cmdlet, it is worth updating for newer versions of Powershell. psm_logger.psm1 currently gives a System.Void error under PS V3 and later. Changing assembly load to

 [Reflection.Assembly]::LoadFrom($log4netDllPath) | Out-Null 

without [Void] resolves the problem.

+2
source

Source: https://habr.com/ru/post/1447518/


All Articles