Reputation: 23
Log4net is working fine on local but on Azure, it only creates files and headers. No logs are written to file. In error it says file is used by another program, I have added . It creates all logs on local.
Framework: .net 4.5, Log4net version : 2.0.3
web.config:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="D:\home\site\wwwroot\log\ErrorLog.log" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n %d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="ALogger" type="HeaderOnceAppender">
<file type="log4net.Util.PatternString" value="D:\home\site\wwwroot\log\A\ALogger_.csv" />
<appendToFile value="true" />
<preserveLogFileNameExtension value="true" />
<rollingStyle value="Date" />
<datePattern value="dd-MM-yyyy-HH" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<header value="Connection Id,Source,DateTime,Event,Logger Source,Stack trace" />
<conversionPattern value="%n%message,%stacktracedetail{5}" />
</layout>
</appender>
<appender name="BLogger" type="HeaderOnceAppender">
<file type="log4net.Util.PatternString" value="D:\home\site\wwwroot\log\B\BLogger_.csv" />
<appendToFile value="true" />
<preserveLogFileNameExtension value="true" />
<rollingStyle value="Date" />
<datePattern value="dd-MM-yyyy" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<header value="Process,Message,DateTime" />
<conversionPattern value="%n%message" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
<logger name="ALogger" additivity="False">
<level value="All" />
<appender-ref ref="ALogger" />
</logger>
<logger name="BLogger" additivity="False">
<level value="All" />
<appender-ref ref="BLogger" />
</logger>
</log4net>
</configuration>
Appender class:
public class HeaderOnceAppender : RollingFileAppender
{
protected override void WriteHeader()
{
if (LockingModel.AcquireLock().Length == 0)
{
base.WriteHeader();
}
}
}
public class xyzOperationLogger
{
public readonly static ILog ALogger;
public readonly static ILog BLogger;
public readonly static ILog System;
static xyzOperationLogger()
{
System = LogManager.GetLogger("System");
ALogger = LogManager.GetLogger("ALogger");
BLogger = LogManager.GetLogger("BLogger");
log4net.Config.XmlConfigurator.Configure();
}
}
Log4net Logs: log4net: Opening file for writing [D:\home\site\wwwroot\log\ErrorLog.log] append [True] log4net:ERROR [FileAppender] ErrorCode: GenericFailure. Unable to acquire lock on file D:\home\site\wwwroot\log\ErrorLog.log. The process cannot access the file 'D:\home\site\wwwroot\log\ErrorLog.log' because it is being used by another process. log4net: Created Appender [LogFileAppender]
log4net: Searched for existing files in [D:\home\site\wwwroot\log\A]
log4net: curSizeRollBackups starts at [0]
log4net: [20-04-2018] vs. [20-04-2018]
log4net: Opening file for writing [D:\home\site\wwwroot\log\A\ALogger_.csv] append [True]
log4net: Created Appender [A]
log4net: Adding appender named [A] to logger [ALogger].
same error for BLogger:
log4net:ERROR [HeaderOnceAppender] ErrorCode: GenericFailure. Unable to acquire lock on file D:\home\site\wwwroot\log\B\BLogger_.csv. The process cannot access the file 'D:\home\site\wwwroot\log\B\BLogger_.csv' because it is being used by another process.
Upvotes: 1
Views: 2224
Reputation: 20067
This problem has been reported by several people as issue LOG4NET-178.
The issue seems to be caused by a broken LOG4NET configuration or a timing problem caused by an application shutdown event that floats in late after an application start event and thus LOG4NET stops logging immediately after it has been started.
The first thing step to troubleshoot problems is enabling the log4net internal debugging features as described here and fix all errors that pop up.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
If the problem still persists, this comment suggests to move the LOG4NET configuration out of the web.config into a separate file like log4net.config.
Finally, if both previous steps did not help and the problem still occurs, you can try to work around the event timing problem by invoking the configuration call from the class constructor as described in this comment.
Also, if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed.
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
And make any call to the log (even one that is filtered or evaluated out of the append process, it should force log4net to evaluate your repository/heirarchy)
static Program()
{
Log.Debug("Application loaded.");
}
Don't forget to add <log4net debug="true">
this line in your configuration.
For more details, refer to this thread.
Upvotes: 2
Reputation: 27944
One of the problems can be cause by the order of confirmation:
static xyzOperationLogger()
{
System = LogManager.GetLogger("System");
ALogger = LogManager.GetLogger("ALogger");
BLogger = LogManager.GetLogger("BLogger");
log4net.Config.XmlConfigurator.Configure();
}
Configure the loggers after you have loaded the configuration:
static xyzOperationLogger()
{
log4net.Config.XmlConfigurator.Configure();
System = LogManager.GetLogger("System");
ALogger = LogManager.GetLogger("ALogger");
BLogger = LogManager.GetLogger("BLogger");
}
Upvotes: 0