Log4net在本地工作正常,但在Azure上,它只创建文件和标题。没有日志写入文件。错误地说,文件被另一个程序使用,我已经添加了。它会在本地创建所有日志。
框架:.net 4.5, Log4net版本: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课程:
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日志: log4net:用于写入的打开文件[D:\ home \ site \ wwwroot \ log \ ErrorLog.log]追加[True] log4net:ERROR [FileAppender] ErrorCode:GenericFailure。无法获取文件D:\ home \ site \ wwwroot \ log \ ErrorLog.log上的锁定。该进程无法访问文件&#; D:\ home \ site \ wwwroot \ log \ ErrorLog.log&#39;因为它正被另一个进程使用。 log4net:创建了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.
答案 0 :(得分:2)
有些人已将此问题报告为问题LOG4NET-178。
这个问题似乎是由应用程序启动事件后的应用程序关闭事件引起的损坏的LOG4NET配置或计时问题引起的,因此LOG4NET它在启动后立即停止记录。
解决问题的第一步是启用此处所述的log4net内部调试功能并修复弹出的所有错误。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
如果问题仍然存在,则comment建议将LOG4NET配置从web.config中移出到一个单独的文件,如log4net.config。
最后,如果前两个步骤都没有帮助并且问题仍然存在,您可以尝试通过调用类构造函数中的配置调用来解决事件计时问题,如comment中所述。
此外,如果使用配置属性,则必须调用log4net以允许它读取属性。对LogManager.GetLogger的简单调用将导致读取和处理调用程序集上的属性。
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
对日志进行任何调用(即使是在追加过程中过滤或评估的日志,也应强制log4net评估您的存储库/层次结构)
static Program()
{
Log.Debug("Application loaded.");
}
不要忘记在配置中添加<log4net debug="true">
这一行。
有关详细信息,请参阅此thread。
答案 1 :(得分:0)
其中一个问题可能是由确认顺序引起的:
static xyzOperationLogger()
{
System = LogManager.GetLogger("System");
ALogger = LogManager.GetLogger("ALogger");
BLogger = LogManager.GetLogger("BLogger");
log4net.Config.XmlConfigurator.Configure();
}
加载配置后配置记录器:
static xyzOperationLogger()
{
log4net.Config.XmlConfigurator.Configure();
System = LogManager.GetLogger("System");
ALogger = LogManager.GetLogger("ALogger");
BLogger = LogManager.GetLogger("BLogger");
}