Getting log4net's XML configuration file read in ASP.NET webforms
I'm trying to get log4net working in a 'classic' webforms app, using a log4net XML configuration file that I know is correct, as it is a copy of a file I use successfully with a number of other applications. I have the config.log4net
file in the main site folder (C:\inetpub\wwwroot\
), and I configure log4net in Global.asax.cs
as follows:
protected void Application_Start(Object sender, Even开发者_如何转开发tArgs e)
{
var log = LogManager.GetLogger("SomeWebsite");
XmlConfigurator.Configure(new FileInfo("config.log4net"));
// bind log to the DI container
...
}
Whenever I then use the log
instance (even within Application_Start
) nothing happens, not even an error. I know that if config.log4net
is misconfigured, log4net will silently fail, but I am sure that this isn't the problem. I have a feeling it has to do with FileInfo
's base path being wrong.
What is the best way to configure log4net in this situation?
Try using Server.MapPath if you are going to grab the file that way:
var fi = new FileInfo(Server.MapPath("~/log4net.config"));
Add your Log4Net Configuration in the web.config like this:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\TestProj\\TestLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
精彩评论