-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from microsoft/main
Release
- Loading branch information
Showing
4 changed files
with
147 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
Function Get-NewLoggerInstance { | ||
[CmdletBinding()] | ||
param( | ||
[ValidateScript( { Test-Path $_ })] | ||
[string]$LogDirectory = (Get-Location).Path, | ||
|
||
[ValidateNotNullOrEmpty()] | ||
[string]$LogName = "Script_Logging", | ||
|
||
[bool]$AppendDateTime = $true, | ||
|
||
[bool]$AppendDateTimeToFileName = $true, | ||
|
||
[int]$MaxFileSizeMB = 10, | ||
|
||
[int]$CheckSizeIntervalMinutes = 10, | ||
|
||
[int]$NumberOfLogsToKeep = 10 | ||
) | ||
|
||
$fileName = if ($AppendDateTime) { "{0}_{1}.txt" -f $LogName, ((Get-Date).ToString('yyyyMMddHHmmss')) } else { "$LogName.txt" } | ||
$fullFilePath = [System.IO.Path]::Combine($LogDirectory, $fileName) | ||
|
||
return [PSCustomObject]@{ | ||
FullPath = $fullFilePath | ||
AppendDateTime = $AppendDateTime | ||
AppendDateTimeToFileName = $AppendDateTimeToFileName | ||
MaxFileSizeMB = $MaxFileSizeMB | ||
CheckSizeIntervalMinutes = $CheckSizeIntervalMinutes | ||
NumberOfLogsToKeep = $NumberOfLogsToKeep | ||
BaseInstanceFileName = $fileName.Replace(".txt", "") | ||
Instance = 1 | ||
NextFileCheckTime = ((Get-Date).AddMinutes($CheckSizeIntervalMinutes)) | ||
PreventLogCleanup = $false | ||
LoggerDisabled = $false | ||
} | Write-LoggerInstance -Object "Starting Logger Instance $(Get-Date)" | ||
} | ||
|
||
Function Write-LoggerInstance { | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[object]$LoggerInstance, | ||
|
||
[Parameter(Mandatory = $true, Position = 1)] | ||
[object]$Object | ||
) | ||
process { | ||
if ($LoggerInstance.LoggerDisabled) { return } | ||
|
||
if ($LoggerInstance.AppendDateTime -and | ||
$Object.GetType().Name -eq "string") { | ||
$Object = "[$([System.DateTime]::Now)] : $Object" | ||
} | ||
|
||
$Object | Out-File $LoggerInstance.FullPath -Append | ||
|
||
#Upkeep of the logger information | ||
if ($LoggerInstance.NextFileCheckTime -gt [System.DateTime]::Now) { | ||
return | ||
} | ||
|
||
#Set next update time to avoid issues so we can log things | ||
$LoggerInstance.NextFileCheckTime = ([System.DateTime]::Now).AddMinutes($LoggerInstance.CheckSizeIntervalMinutes) | ||
$item = Get-ChildItem $LoggerInstance.FullPath | ||
|
||
if (($item.Length / 1MB) -gt $LoggerInstance.MaxFileSizeMB) { | ||
$LoggerInstance | Write-LoggerInstance -Object "Max file size reached rolling over" | Out-Null | ||
$directory = [System.IO.Path]::GetDirectoryName($LoggerInstance.FullPath) | ||
$fileName = "$($LoggerInstance.BaseInstanceFileName)-$($LoggerInstance.Instance).txt" | ||
$LoggerInstance.Instance++ | ||
$LoggerInstance.FullPath = [System.IO.Path]::Combine($directory, $fileName) | ||
|
||
$items = Get-ChildItem -Path ([System.IO.Path]::GetDirectoryName($LoggerInstance.FullPath)) -Filter "*$($LoggerInstance.BaseInstanceFileName)*" | ||
|
||
if ($items.Count -gt $LoggerInstance.NumberOfLogsToKeep) { | ||
$item = $items | Sort-Object LastWriteTime | Select-Object -First 1 | ||
$LoggerInstance | Write-LoggerInstance "Removing Log File $($item.FullName)" | Out-Null | ||
$item | Remove-Item -Force | ||
} | ||
} | ||
} | ||
end { | ||
return $LoggerInstance | ||
} | ||
} | ||
|
||
Function Invoke-LoggerInstanceCleanup { | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[object]$LoggerInstance | ||
) | ||
|
||
if ($LoggerInstance.LoggerDisabled -or | ||
$LoggerInstance.PreventLogCleanup) { | ||
return | ||
} | ||
|
||
Get-ChildItem -Path ([System.IO.Path]::GetDirectoryName($LoggerInstance.FullPath)) -Filter "*$($LoggerInstance.BaseInstanceFileName)*" | | ||
Remove-Item -Force | ||
} |
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
8 changes: 4 additions & 4 deletions
8
docs/Admin/Get-SimpleAdminAuditLogReport.md → docs/Admin/Get-SimpleAuditLogReport.md
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