log4net with ASP.NET MVC: nothing happens
I am trying to use log4net with ASP.NET MVC and I cannot get anything to happen with it. I've created a config that is in my web project root:
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="RollingLogFileAppender"/>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:DWSApplicationFilesAppLogsapp.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:DWSApplicationFilesAppLogsapp.log" />
<appendToFile value="false" />
<datePattern value="-dddd" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
</layout>
</appender>
</log4net>
Before I am asked: yes, the application has permission to write to the directory. I have tested this and the application has permission to this directory.
Here is where I am trying to use log4net:
public class HomeController : Controller
{
readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ActionResult Index()
{
log.Error("In Index ");
return View();
}
}
When I run the application and go to this controller log4net does nothing; it doesn't create the files in that directory or anything.
I have enabled internal debugging for log4net and I get no output errors in the console. The following is all I see from log4net:
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:Userstwaldron.BULLFROGSPASAppDataLocalTempTemporary ASP.NET Filesroot7642c99a60feb7f2assemblydl317247033 08dfd6d_e2d0ca01log4net.DLL]. (.NET Runtime [2.0.50727.4952] on Microsoft Windows NT 6.1.7600.0)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:Userstwaldron.BULLFROGSPASAppDataLocalTempTemporary ASP.NET Filesroot7642c99a60feb7f2assemblydl32960c79fb876bb2d_aca7cb01Bullfrog.DWS.Web.DLL]
log4net: DefaultRepositorySelector: Assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] 'WebDev.WebServer20.EXE' (Managed (v2.0.50727)): Loaded 'Anonymously Hosted DynamicMethods Assembly'
在global.asax中有这样的东西吗?
void Application_Start(object sender, EventArgs e)
{
string l4net = Server.MapPath("~/log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
...
Ensure that you call
log4net.Config.XmlConfigurator.Configure();
At the startup of your application.
Confuguring Log4Net in ASP.NET MVC 4 Web Application
Download Log4Net:
Add reference in ASP.Net MVC 4 application:
Modify Web.config file (source):
<configuration>
<configSections>
<section
name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="LOGSlog.txt"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<logger name="test">
<level value="DEBUG"></level>
</logger>
</log4net>
</configuration>
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
}
readonly log4net.ILog log =
log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
);
log.Error("make mistakes");
Further reading: http://anandsonake.blogspot.in/2013/08/log4net-in-aspnet-mvc.html
链接地址: http://www.djcxy.com/p/43650.html