I have the following Powershell 7 code to configure a RollingFileAppender:
function Get-RollingFileAppender {
#####################################################################################################################
#RollingFileAppender (named RollingLogFileAppender)
#
#Define Values for RollingFileAppender Configuration
#####################################################################################################################
# set the logfile name, determine append mode, logging pattern, create a file appender and a color console appender
try {
$LogFile = ( [IO.Path]::Combine($PWD, 'logs', ('LogFile_{0:yyyy-MM-dd}_{0:HH-mm-ss}.log' -f (Get-Date) )) )
("Setting the RollingLogFileAppender logfile - name {0}" -f $LogFile) | Write-Debug
$RollingLogFileAppender = New-Object log4net.Appender.RollingFileAppender
if ($null -eq $RollingLogFileAppender) {
throw 'RollingLogFileAppender could not be created'
}
#Set RollingLogFileAppender Configuration
$RollingLogFileAppender.File = $LogFile
#File locking model
#must create a locking object of the desired locking type
$RollingLogFileAppender.LockingModel = [log4net.Appender.FileAppender+MinimalLock]::new()
# pattern
$Pattern = '[%date{yyyy-MM-dd HH:mm:ss.fff} (%utcdate{yyyy-MM-dd HH:mm:ss.fff})] [%level] [%message]%n'
$PatternLayout = [log4net.Layout.ILayout](New-Object log4net.Layout.PatternLayout($Pattern))
$RollingLogFileAppender.Layout = $PatternLayout
$PatternLayout.ActivateOptions()
$RollingLogFileAppender.Threshold = [log4net.Core.Level]::All
$AppendToFile = $True
$RollingLogFileAppender.AppendToFile = $AppendToFile
$MaximumFileSize = 5 * [Math]::Pow(1024,2) # 5 megabytes
$RollingLogFileAppender.MaxFileSize = $MaximumFileSize
$MaxSizeRollBackups = 5
$RollingLogFileAppender.MaxSizeRollBackups = $MaxSizeRollBackups
$RollingLogFileAppender.ActivateOptions()
'Configure the RollingLogFileAppender' | Write-Debug
[log4net.Config.BasicConfigurator]::Configure($RollingLogFileAppender)
'Adding the RollingLogFileAppender to the repository' | Write-Debug
$Global:repository.Root.AddAppender($RollingLogFileAppender)
'Success - Created the RollingLogFileAppender' | Write-Debug
}
catch {
Write-Error $_.Exception.ToString()
}
#####################################################################################################################
}
I have struggled to get this code to actually set the LockingModel to Minimal, but the solution in the code above seems to address that. What concerns me is whether this is the correct way to do this or is there a different way to set the locking model dynamically?
Please do note that I have a config file section that is largely redundant with this. It establishes the ability to change the configuration at will as these settings are applied late in the process of creating the appender listed earlier:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingmodel type="log4net.Appender.FileAppender+MinimalLock">
<param name="File" type="log4net.Util.PatternString" value="%property{LogFileName}" />
<!-- <param name="File" value="./Logs/EGNYTE-Processing.log"/> -->
<param name="AppendToFile" value="true"/>
<param name="MaxSizeRollBackups" value="5"/>
<param name="MaximumFileSize" value="5MB"/>
<param name="RollingStyle" value="Size"/>
<param name="StaticLogFileName" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<!-- show activity id to differentiate origin-->
<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c [%property{activity}] - %m%n"/>
<!-- basic layout -->
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c [%ndc] - %m%n" />-->
<!-- uncomment the next one for way too much information...-->
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c [%ndc] - %m%n - %property{activity} - %property{requestinfo}"/>--></layout>
</appender>