From 7aa1b7c0bc3283aa5891cff7435948bc2764e743 Mon Sep 17 00:00:00 2001 From: "Soennecken, Torben" Date: Mon, 15 Feb 2021 13:36:25 +0100 Subject: [PATCH] [+] Added target initialization for File. It tries to initially create the directory for the log target. This should be useful for e.g. nested log directories C:\log\module\monday\... --- Logging/targets/File.ps1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Logging/targets/File.ps1 b/Logging/targets/File.ps1 index 0637547..d03d8f1 100644 --- a/Logging/targets/File.ps1 +++ b/Logging/targets/File.ps1 @@ -9,7 +9,20 @@ Level = @{Required = $false; Type = [string]; Default = $Logging.Level } Format = @{Required = $false; Type = [string]; Default = $Logging.Format } } + Init = { + param( + [hashtable] $Configuration + ) + [String] $directoryPath = [System.IO.Path]::GetDirectoryName($Configuration.Path) + + # We (try to) create the directory if it is not yet given + if (-not [System.IO.Directory]::Exists($directoryPath)){ + # "Creates all directories and subdirectories in the specified path unless they already exist." + # https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory?view=net-5.0#System_IO_Directory_CreateDirectory_System_String_ + [System.IO.Directory]::CreateDirectory($directoryPath) | Out-Null + } + } Logger = { param( [hashtable] $Log,