Skip to content

Instantly share code, notes, and snippets.

@Robert-LTH
Created February 8, 2019 23:19
Show Gist options
  • Save Robert-LTH/208365d0df93ce43f3917025d44b83b8 to your computer and use it in GitHub Desktop.
Save Robert-LTH/208365d0df93ce43f3917025d44b83b8 to your computer and use it in GitHub Desktop.
function Load-Module {
param(
$ModuleName,
[switch]$Force
)
$Params = @{
ErrorAction = 'Stop'
}
if ($Force.IsPresent) {
$Params.Add('Force',$true)
}
if (-not (Get-Module -Name $ModuleName) -or $Force.IsPresent) {
try {
Import-Module @Params -Name $ModuleName
} catch {
throw "Failed to load module '$ModuleName'."
}
}
}
function Init-Logging {
param(
$LogFilePath
)
# https://github.com/EsOsO/Logging
Load-Module -ModuleName 'Logging' -Force
# CMTrace format
Set-LoggingDefaultFormat -Format "<![LOG[%{message}]LOG]!><time=`"%{timestamp:+%T.000}+000`" date=`"%{timestamp:+%m-%d-%Y}`" component=`"%{body}`" context=`"asd`" type=`"%{levelno}`" thread=`"%{pid}`" file=`"%{Caller}@%{lineno}`">"
# CMTrace LoggingLevel
Add-LoggingLevel -Level 2 -LevelName 'CCMWARNING'
Add-LoggingLevel -Level 3 -LevelName 'CCMERROR'
Add-LoggingTarget -Name File -Configuration @{Path = $LogFilePath}
}
Init-Logging -LogFilePath 'C:\temp\scriptlog.log'
1..3 | % {
Write-Log -Level CCMERROR -Message "Testing Error!" -Body 'This-Function'
Write-Log -Level CCMWARNING -Message "Testing Warning!" -Body 'This-Function'
Write-Log -Body 'This-Function' -Message "Testing!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment