Reputation: 100
So, I have a project in git repo, and 2 branches: master and fix-master. I've not changed code associated with log4net in fix-master. But every time, when I've started master - I don't have this problem, if I checkout to fix-master, I have this error after this line:
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
Upvotes: 1
Views: 3709
Reputation: 590
The debugging directive does help. FWIW, here is Powershell code with an explicit step to set the RollingFileAppender locking model. Tested with Powershell 7, using log4net.Core. Presumably can be adapted for other languages.
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()
}
#####################################################################################################################
}
Upvotes: 0
Reputation: 121619
Aside from the obvious candidates (illegal filepath, permissions error), the problem could even be mutex locking:
https://issues.apache.org/jira/browse/LOG4NET-506
RollingFileAppender locking log folder in some cases
Abstract:
In some configuration cases, the rolling file appender may issue a mutex lock on the folder name. If more than one processes try to do that with different executing identity, only one will be able to log to its specific logs file. The others will no more be able to do it.
SUGGESTION:
Add this line to your app.config/web.config, under appSettings, to enable internal debugging:
<add key="log4net.Internal.Debug" value="true"/>
This will give you output like this, to both the system console and to System.Diagnostics.Trace
log4net:ERROR Could not create Appender [RollingLogFileAppender] of type [log4net.Appender.RollingFileAppender]. Reported error follows.
System.UnauthorizedAccessException: Access to path 'D__Logs_' denied. (Translated from french)
à System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
à System.Threading.Mutex.MutexTryCodeHelper.MutexTryCode(Object userData)
à System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
à System.Threading.Mutex.CreateMutexWithGuaranteedCleanup(Boolean initiallyOwned, String name, Boolean& createdNew, SECURITY_ATTRIBUTES secAttrs)
à System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity)
à System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew)
à log4net.Appender.RollingFileAppender.ActivateOptions()
à log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [RollingLogFileAppender] not found.
Please try this, and post back what you find!
Upvotes: 1