Utilizzando Log4Net in PowerShell

voti
0

Sto solo cercando semplice esempio di utilizzo di log4net in uno script PowerShell. Ho veramente provato googling questo e francamente sono abbastanza confuso a questo punto.

È pubblicato 13/02/2020 alle 21:55
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Beh, sembra che ho formulato la mia domanda poco. Ho continuato a trovare questo link PowerShell-logging-con-apache-log4net , ma l'ho trovato davvero difficile da leggere.

Così, ho preso questo codice e scritto in modo più amichevole (per me). L'intera ColoredConsoleAppender

#####################################################################################################################
#Load log4net.dll
$DllPath = ([System.IO.Directory]::GetParent($MyInvocation.MyCommand.Path)).FullName
$DllFile = Resolve-Path (Join-Path -Path $DllPath -ChildPath 'log4net.dll')
[Void][Reflection.Assembly]::LoadFile($DllFile)
#####################################################################################################################
#FileAppender
#
#Define Values for FileAppender Configuration 
$Pattern = '[%date{yyyy-MM-dd HH:mm:ss.fff} (%utcdate{yyyy-MM-dd HH:mm:ss.fff})] [%level] [%message]%n'
$PatternLayout = [log4net.Layout.ILayout](new-object log4net.Layout.PatternLayout($Pattern))

$LogPath = ([System.IO.Directory]::GetParent($MyInvocation.MyCommand.Path)).FullName
$LogFile = Join-Path -Path $LogPath -ChildPath $('LogFile_{0:yyyy-MM-dd}_{0:HH-mm-ss}.log' -f (Get-Date))

$AppendToFile = $True

#Load FileAppender Configuration
$FileAppender = new-object log4net.Appender.FileAppender($PatternLayout,$LogFile,$AppendToFile);
$FileAppender.Threshold = [log4net.Core.Level]::All;
[log4net.Config.BasicConfigurator]::Configure($FileAppender);
#####################################################################################################################
#ColoredConsoleAppender
#
#Define Values for ColoredConsoleAppender Configuration 
$Pattern = '[%date{yyyy-MM-dd HH:mm:ss.fff}] %message%n'
$PatternLayout = [log4net.Layout.ILayout](new-object log4net.Layout.PatternLayout($Pattern))
$ColoredConsoleAppender = new-object log4net.Appender.ColoredConsoleAppender($PatternLayout)

$ColoredConsoleAppenderLevelAndColors = new-object log4net.Appender.ColoredConsoleAppender+LevelColors

$ColoredConsoleAppenderLevelAndColors.Level = [log4net.Core.Level]::Debug
$ColoredConsoleAppenderLevelAndColors.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::Green
$ColoredConsoleAppender.AddMapping($ColoredConsoleAppenderLevelAndColors)

$ColoredConsoleAppenderLevelAndColors.level = [log4net.Core.Level]::Info
$ColoredConsoleAppenderLevelAndColors.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::White
$ColoredConsoleAppender.AddMapping($ColoredConsoleAppenderLevelAndColors)

$ColoredConsoleAppenderLevelAndColors.level=[log4net.Core.Level]::Warn
$ColoredConsoleAppenderLevelAndColors.ForeColor=[log4net.Appender.ColoredConsoleAppender+Colors]::Yellow
$ColoredConsoleAppender.AddMapping($ColoredConsoleAppenderLevelAndColors)

$ColoredConsoleAppenderLevelAndColors.level=[log4net.Core.Level]::Error
$ColoredConsoleAppenderLevelAndColors.ForeColor=[log4net.Appender.ColoredConsoleAppender+Colors]::Red
$ColoredConsoleAppender.AddMapping($ColoredConsoleAppenderLevelAndColors)

$ColoredConsoleAppenderLevelAndColors.level=[log4net.Core.Level]::Fatal
$ColoredConsoleAppenderLevelAndColors.ForeColor=[log4net.Appender.ColoredConsoleAppender+Colors]::HighIntensity -bxor [log4net.Appender.ColoredConsoleAppender+Colors]::Red
$ColoredConsoleAppender.AddMapping($ColoredConsoleAppenderLevelAndColors)

$ColoredConsoleAppender.ActivateOptions();

#Load FileAppender Configuration
$ColoredConsoleAppender.Threshold = [log4net.Core.Level]::All;
[log4net.Config.BasicConfigurator]::Configure($ColoredConsoleAppender);
#####################################################################################################################
$Log=[log4net.LogManager]::GetLogger("root");
$Log.Debug(‘Debug message.’);
$Log.Info(‘Info message.’);
$Log.Warn(‘Warn message.’);
$Log.Error(‘Error message.’);
$Log.Fatal(‘Fatal message.’);
[log4net.LogManager]::ResetConfiguration();
Risposto il 14/02/2020 a 20:34
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more