为另一个log4net配置问题道歉。 我有一个具有此方法的引用库:
public static ILog Logger = LogManager.GetLogger(typeof(MyClass));
public void InitializeLog()
{
XmlConfigurator.Configure(new FileInfo("log4net.xml"));
Logger.Info("Starting");
}
在使用此DLL的项目中,我在项目的根级别有log4net.xml。它看起来像这样:
<log4net>
<logger name="NHibernate">
<level value="DEBUG" />
</logger>
<logger name="NHibernate.SQL">
<level value="DEBUG" />
</logger>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level - %message%newline%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\log\project.log" />
<appendToFile value="true" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<footer value =" "/>
<conversionPattern value="%date %level - %message%newline%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
</log4net>
项目在尝试记录任何内容之前调用InitializeLog()
。然而,它甚至不会记录&#34;开始&#34;开头的消息。当我调试方法时,在Logger对象中我看到Configured = false
。我觉得它与xml文件的路径有关,因为它在使用这个库作为项目引用而不是外部引用的项目中工作正常(这里不是选项。)关于配置失败原因的想法?
答案 0 :(得分:0)
当前文件夹中的log4net搜索配置。您需要指定配置文件的完整路径:
ASP.NET应用程序
protected void Application_Start()
{
...
string l4net = Server.MapPath("~/log4net.config");
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(l4net));
}
WinForm或控制台应用程序:
使用输出目录的完整路径(您可以从另一个文件夹调用您的exe)
static void Main(string[] args)
{
var appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(appPath));
}
我的log4net.config的内容:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="osai.log" />
<appendToFile value="true" />
<encoding value="utf-8" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="50MB" />
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="--- Application start ---
" />
<param name="Footer" value="--- Application stop ---
" />
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>