无法让 NHibernate 和 Log4Net 生成日志记录。

发布于 2024-08-18 02:44:52 字数 3977 浏览 4 评论 0原文

我无法获取 NHibernate 或我的应用程序的任何日志记录。我尝试了我能想到的一切,但没有任何效果!

这是我的代码:

using System.Reflection;
using NHibernate.Cfg;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            var cfg = new Configuration()
                    .Configure()
                    .AddAssembly(Assembly.GetCallingAssembly());
        }
    }
}



namespace NHibernate_Log4Net.Model
{
    public class Item
    {
        public int Id { get; set; }
        public int Title { get; set; }
        public int Alias { get; set; }
    }
}

Item.hbm.xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate_Log4Net.Model.Item" assembly="NHibernate_Log4Net" auto-import="false">
  <class name="User" table="Users">
    <id name="Id">
      <generator class="Native"/>
    </id>

    <property name="Title" length="255" not-null="true" />
    <property name="Alias" length="255" not-null="true" />
  </class>
</hibernate-mapping>

Log4Net.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="false">

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log4netLogger.log" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>

  <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %m - %c -%n" />
    </layout>
  </appender>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline"/>
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="OutputDebugStringAppender" />
    <appender-ref ref="TraceAppender" />
  </root>
  <logger name="NHibernate" additivity="false">
    <level value="FATAL"/>
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="OutputDebugStringAppender" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="TraceAppender" />
  </logger>
  <logger name="NHibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </logger>
  <!-- 
      NHibernate.Loader.Loader logs diagnostic stuff and SELECTs. 
      You can use either logger, or both, depending on you needs.
    -->
  <logger name="NHibernate.Loader.Loader" additivity="false">
    <level value="INFO" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </logger>


</log4net>

NHibernate 抛出一个错误,表明类 Users 不存在。这是预料之中的,但为什么我没有看到来自 NHibernate 的任何日志记录?

(我可以自己记录它,但我看不到 NHibernate 的任何日志)。

I can't get any logging of NHibernate or my application. I tried EVERYTHING that I could think about, but nothing works!

Here is my code:

using System.Reflection;
using NHibernate.Cfg;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            var cfg = new Configuration()
                    .Configure()
                    .AddAssembly(Assembly.GetCallingAssembly());
        }
    }
}



namespace NHibernate_Log4Net.Model
{
    public class Item
    {
        public int Id { get; set; }
        public int Title { get; set; }
        public int Alias { get; set; }
    }
}

Item.hbm.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate_Log4Net.Model.Item" assembly="NHibernate_Log4Net" auto-import="false">
  <class name="User" table="Users">
    <id name="Id">
      <generator class="Native"/>
    </id>

    <property name="Title" length="255" not-null="true" />
    <property name="Alias" length="255" not-null="true" />
  </class>
</hibernate-mapping>

Log4Net.config file:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="false">

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log4netLogger.log" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>

  <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %m - %c -%n" />
    </layout>
  </appender>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline"/>
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="OutputDebugStringAppender" />
    <appender-ref ref="TraceAppender" />
  </root>
  <logger name="NHibernate" additivity="false">
    <level value="FATAL"/>
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="OutputDebugStringAppender" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="TraceAppender" />
  </logger>
  <logger name="NHibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </logger>
  <!-- 
      NHibernate.Loader.Loader logs diagnostic stuff and SELECTs. 
      You can use either logger, or both, depending on you needs.
    -->
  <logger name="NHibernate.Loader.Loader" additivity="false">
    <level value="INFO" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </logger>


</log4net>

NHibernate throws an error that the class Users doesn't exist. This is expected, but why do I not see any logging from this, from NHibernate?

(I can log it myself, but the point that I can't see any log from NHibernate).

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

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

发布评论

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

评论(4

芸娘子的小脾气 2024-08-25 02:44:52

配置文件需要与可执行文件匹配,您的可执行文件名为 Log4Net.exe 吗?

The config file needs to match the executable, is your executable named Log4Net.exe?

天煞孤星 2024-08-25 02:44:52

我过去在使用 XmlConfigurator 属性时遇到了麻烦。也许您应该在 main 中显式调用 XmlConfigurator,如下所示:

namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
            var cfg = new Configuration()
                    .Configure()
                    .AddAssembly(Assembly.GetCallingAssembly());
        }
    }
}

I have had trouble using the XmlConfigurator attribute in the past. Perhaps you should just call XmlConfigurator explicitly in your main like so:

namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
            var cfg = new Configuration()
                    .Configure()
                    .AddAssembly(Assembly.GetCallingAssembly());
        }
    }
}
画尸师 2024-08-25 02:44:52

如果为 NHibernate 记录器设置的日志记录级别太高(或关闭),您可能看不到来自 NHibernate 的任何日志消息。例如,的值是多少
((log4net.Repository.Hierarchy.Logger)LogManager.GetLogger("NHibernate").Logger).Level

您的配置文件中应该有一个如下所示的部分:

<log4net>
        <appender name="NHLog" type="log4net.Appender.FileAppender">
                <file value="logs/nhibernate.log" />
                <appendToFile value="false" />
                <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
                </layout>
        </appender>
        <logger name="NHibernate" additivity="false">
                <level value="DEBUG"/>
                <appender-ref ref="NHLog"/>
        </logger>
</log4net>

更新: 如果日志文件始终创建(运行前不存在时)但始终为空,则这表明在附加过程中出现异常。为了最大限度地减少异常的可能性,请使用非常简单的 conversionPattern(可能只是 "%m%n")并查看是否生成任何输出。如果有,请添加回转换模式的元素,直到找到问题所在。如果没有生成输出,请从您自己的代码中将某些内容记录到 NHibernate 记录器(这完全没问题),并在调试器中单步执行它。

ILog log = LogManager.GetLogger("NHibernate");

log.Info("Application starting");

如果仍然不满意,您可能需要发布一些代码/配置。

If the logging levels set for the NHibernate loggers are too high (or off), you may not see any log messages from NHibernate. For example, what is the value of
((log4net.Repository.Hierarchy.Logger)LogManager.GetLogger("NHibernate").Logger).Level
?

You should have a section in your config file like this:

<log4net>
        <appender name="NHLog" type="log4net.Appender.FileAppender">
                <file value="logs/nhibernate.log" />
                <appendToFile value="false" />
                <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
                </layout>
        </appender>
        <logger name="NHibernate" additivity="false">
                <level value="DEBUG"/>
                <appender-ref ref="NHLog"/>
        </logger>
</log4net>

Update: If the log file is always created (when non-existent before a run) but is always empty, this points to an exception during appending. To minimise the possibility of an exception, have a very simple conversionPattern (perhaps just "%m%n") and see if any output is generated. If there is, add back elements of the conversion pattern until you find where the problem is. If no output is generated, log something to the NHibernate logger from your own code (this is perfectly OK) and step through it in the debugger.

ILog log = LogManager.GetLogger("NHibernate");

log.Info("Application starting");

If still no joy, you may have to post some of your code/configuration.

夜声 2024-08-25 02:44:52

https://web.archive.org/web/20110514164829/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/07/01/how- to-configure-log4net-for-use-with-nhibernate.aspx

我使用上面的链接来设置日志记录。尝试以下

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            var cfg = new Configuration()
                    .AddAssembly(Assembly.GetCallingAssembly());
                    .Configure()

        }
    }
}

额外的是这一行

log4net.Config.XmlConfigurator.Configure();

它对我不起作用,直到我添加它。我知道这有点类似于 Jeffery 的建议,但是……只是我的 2 美分。

另外,对 Nhibernate 配置对象的Configure() 调用不应该放在最后吗?添加程序集后?

https://web.archive.org/web/20110514164829/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/07/01/how-to-configure-log4net-for-use-with-nhibernate.aspx

I used the above link to set up logging. Try the following

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace NHibernate_Log4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            var cfg = new Configuration()
                    .AddAssembly(Assembly.GetCallingAssembly());
                    .Configure()

        }
    }
}

The extra is this line

log4net.Config.XmlConfigurator.Configure();

It didnt work for me till I added that. I know this is somewhat similar to what Jeffery suggested, but... just my 2cents.

Also, shouldnt the Configure() call on the Nhibernate configuration object come last? After the Assembly is added?

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