提问人:Allen 提问时间:10/27/2023 更新时间:10/27/2023 访问量:10
在 POWERSHELL 7 中动态设置 log4net rollingfileappender 的锁定级别
Setting locklevel for log4net rollingfileappender dynamically in POWERSHELL 7
问:
我有以下 Powershell 7 代码来配置 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()
}
#####################################################################################################################
}
我一直在努力让这段代码实际将 LockingModel 设置为 Minimal,但上面代码中的解决方案似乎解决了这个问题。我担心的是,这是否是正确的方法,或者是否有不同的方法来动态设置锁定模型?
请注意,我有一个配置文件部分,它在很大程度上是多余的。它建立了随意更改配置的能力,因为这些设置在创建前面列出的 appender 的过程中应用:
<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>
答: 暂无答案
评论