未创建log4net日志文件

时间:2011-04-19 10:14:49

标签: nhibernate log4net

在我的项目中,我使用MVC,NHibernate并记录html错误,我想使用log4net库。

这是web.config配置

<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>

在AssemblyInfo.cs文件中我有:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

但是在控制器中,当我使用这部分代码时,它不会创建任何txt文件。

 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);

有什么问题?

1 个答案:

答案 0 :(得分:0)

尝试在根元素下添加:

<logger name="NHibernate.SQL">
   <appender-ref ref="rollingFile" />
   <level value="ALL" />
</logger>

修改

要使用名为MainLogger的记录器,请使用:

<logger name="MainLogger">
   <appender-ref ref="rollingFile" />
   <level value="ALL" />
</logger>

在调试模式下,文件将在<projectfolder>\bin\Debug中创建,因为您没有指定路径而只指定文件名。