Last active
July 17, 2021 09:34
-
-
Save RafPe/95ef838c51edb7ef43c4 to your computer and use it in GitHub Desktop.
Nlog logging with PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$dllBytes = [System.IO.File]::ReadAllBytes( "C:\NLog.dll") | |
[System.Reflection.Assembly]::Load($dllBytes) | |
# Create object that we will use to send customized info | |
# This is to be used if you use ${event-context:item=MyValue} in your config ! | |
$cos = new-object NLog.LogEventInfo | |
$cos.Level = [NLog.LogLevel]::Info | |
$cos.Message = 'executing something really cool' | |
# Load XML configuration and use it | |
$xmlConfig = New-Object NLog.Config.XmlLoggingConfiguration("\\pathToConfig\NLog.config") | |
[NLog.LogManager]::Configuration = $xmlConfig | |
# Create logger | |
$logger_psmodule = [NLog.LogManager]::GetLogger('logger.name') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create file target | |
$target = New-Object NLog.Targets.FileTarget | |
# Define layout | |
$target.Layout = 'timestamp=${longdate} host=${machinename} logger=${logger} loglevel=${level} messaage=${message}' | |
$target.FileName = 'D:\Tools\${date:format=yyyyMMdd}.log' | |
$target.KeepFileOpen = $false | |
# Init config | |
$config = new-object NLog.Config.LoggingConfiguration | |
# Add target | |
$config.AddTarget('File',$target) | |
# Add rule for logging | |
$rule1 = New-Object NLog.Config.LoggingRule('*', [NLog.LogLevel]::Info,$target) | |
$config.LoggingRules.Add($rule1) | |
# Add rule for logging | |
$rule2 = New-Object NLog.Config.LoggingRule('*', [NLog.LogLevel]::Off,$target) | |
$config.LoggingRules.Add($rule2) | |
# Add rule for logging | |
$rule3 = New-Object NLog.Config.LoggingRule('*', [NLog.LogLevel]::Error,$target) | |
$config.LoggingRules.Add($rule3) | |
# Save config | |
[NLog.LogManager]::Configuration = $config | |
$logger = [NLog.LogManager]::GetLogger('logger.name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<targets> | |
<target name="csv" xsi:type="File" fileName="c:\\psaudit.${windows-identity:domain=False}-${shortdate}.csv"> | |
<layout xsi:type="CSVLayout"> | |
<column name="timestamp" layout="${longdate}" /> | |
<column name="servername" layout="${machinename}" /> | |
<column name="message" layout="${message}" /> | |
<column name="logger" layout="${logger}"/> | |
<column name="level" layout="${level}"/> | |
<column name="username" layout="${windows-identity}"/> | |
</layout> | |
</target> | |
</targets> | |
<rules> | |
<logger name="*" minlevel="Info" writeTo="csv" /> | |
</rules> | |
</nlog> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment