log4net log file is not created
In my project I'm using MVC, NHibernate and for logging html errors I wanna use log4net library.
Here is web.config
configuration
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<!--<sectionGroup name="blowery.web">
<section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>-->
</configSections>
<log4net debug="false">
<!-- Define some output appenders -->
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="INFO" />
<appender-ref ref="console" />
</root>
</log4net>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Password=maserver.login12345;Persist Security Info=True;User ID=sa;Initial Catalog=Mashhad4;Data Source=DEVSERVER</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<!--<property name="hibernate.batch_size">20</property>-->
<!--<property name="command_timeout">5000</property>-->
</session-factory>
</hibernate-configuration>
<configuration>
In AssemblyInfo.cs file I have this:
[assembly: 开发者_JAVA技巧log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
But in the controller, when I use this part of code, it doesn't create any txt file.
log4net.Config.BasicConfigurator.Configure();
Exception inner = filterContext.Exception.InnerException;
log4net.ILog log = log4net.LogManager.GetLogger("MainLogger");
log.Error("Unhandled exception", inner == null ? filterContext.Exception : inner);
What is the problem?
Try adding this under the root element:
<logger name="NHibernate.SQL">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
Edit:
In order to use the logger called MainLogger, use this:
<logger name="MainLogger">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
When in Debug mode, the file will be created in <projectfolder>\bin\Debug
, since you did not specify a path but only the filename.
精彩评论