Skip to content

Instantly share code, notes, and snippets.

@iOnline247
Created April 23, 2020 05:11
Show Gist options
  • Save iOnline247/f07199cdd8e58725faa4e16c4794e53d to your computer and use it in GitHub Desktop.
Save iOnline247/f07199cdd8e58725faa4e16c4794e53d to your computer and use it in GitHub Desktop.
log4net in PowerShell
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Push-Location $scriptDir
Add-Type -Path "$scriptDir\dll\log4net.dll"
$FileAppender = New-Object log4net.Appender.FileAppender(
(
[log4net.Layout.ILayout](New-Object log4net.Layout.PatternLayout('%date{yyyy-MM-dd HH:mm:ss.fff} | %level | %property{correlationId} | [%thread] | Department | App_Name | %c | $property{jobId} | %message%n')),
"$scriptDir\LogOutputInConsoleAndFile.log", # $LogFileName
$True
)
);
$FileAppender.Threshold = [log4net.Core.Level]::Debug; # $LogLevel
[log4net.Config.BasicConfigurator]::Configure($FileAppender);
# Colored Console Appender
# $ColConsApndr = new-object log4net.Appender.ColoredConsoleAppender(([log4net.Layout.ILayout](new-object log4net.Layout.PatternLayout('[%date{yyyy-MM-dd HH:mm:ss.fff}] %message%n'))));
# $ColConsApndrDebugCollorScheme = new-object log4net.Appender.ColoredConsoleAppender+LevelColors; $ColConsApndrDebugCollorScheme.Level = [log4net.Core.Level]::Debug; $ColConsApndrDebugCollorScheme.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::Green;
# $ColConsApndr.AddMapping($ColConsApndrDebugCollorScheme);
# $ColConsApndrInfoCollorScheme = new-object log4net.Appender.ColoredConsoleAppender+LevelColors; $ColConsApndrInfoCollorScheme.level = [log4net.Core.Level]::Info; $ColConsApndrInfoCollorScheme.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::White;
# $ColConsApndr.AddMapping($ColConsApndrInfoCollorScheme);
# $ColConsApndrWarnCollorScheme = new-object log4net.Appender.ColoredConsoleAppender+LevelColors; $ColConsApndrWarnCollorScheme.level = [log4net.Core.Level]::Warn; $ColConsApndrWarnCollorScheme.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::Yellow;
# $ColConsApndr.AddMapping($ColConsApndrWarnCollorScheme);
# $ColConsApndrErrorCollorScheme = new-object log4net.Appender.ColoredConsoleAppender+LevelColors; $ColConsApndrErrorCollorScheme.level = [log4net.Core.Level]::Error; $ColConsApndrErrorCollorScheme.ForeColor = [log4net.Appender.ColoredConsoleAppender+Colors]::Red;
# $ColConsApndr.AddMapping($ColConsApndrErrorCollorScheme);
# $ColConsApndrFatalCollorScheme = new-object log4net.Appender.ColoredConsoleAppender+LevelColors; $ColConsApndrFatalCollorScheme.level = [log4net.Core.Level]::Fatal; $ColConsApndrFatalCollorScheme.ForeColor = ([log4net.Appender.ColoredConsoleAppender+Colors]::HighIntensity -bxor [log4net.Appender.ColoredConsoleAppender+Colors]::Red);
# $ColConsApndr.AddMapping($ColConsApndrFatalCollorScheme);
# $ColConsApndr.ActivateOptions();
# $ColConsApndr.Threshold = [log4net.Core.Level]::All;
# [log4net.Config.BasicConfigurator]::Configure($ColConsApndr);
try {
$logger = [log4net.LogManager]::GetLogger('My_Component');
[log4net.LogicalThreadContext]::Properties['correlationId'] = (New-Guid).ToString()
[log4net.LogicalThreadContext]::Properties['jobId'] = (New-Guid).ToString()
$logger.Debug('Debug log.')
$logger.Info('Info log.')
$logger.Warn('Warn log.')
try {
throw 'WHOOPS'
}
catch [Exception] {
$logger.Error('Error log.', $_.Exception)
}
$logger.Fatal('Fatal log.')
}
finally {
[log4net.LogManager]::Shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment