log4net 日志文件未创建

发布于 2024-11-02 09:11:18 字数 3335 浏览 1 评论 0原文

在我的项目中,我使用 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);

问题是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情 2024-11-09 09:11:18

尝试在根元素下添加此内容:

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

编辑:

为了使用名为 MainLogger 的记录器,请使用以下内容:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文