使用 log4net 清空 NHibernate 日志文件

发布于 2024-08-09 21:59:01 字数 2076 浏览 10 评论 0 原文

我正在尝试使用 Fluent NHibernate 和 log4net 在我的应用程序上启用日志记录。我已经尝试过这里描述的事情,此处这里此处。正在创建日志文件,但没有向其中写入任何内容。此应用程序和其他应用程序的其他日志文件都工作正常,因此我假设问题出在我的配置上。

以下是我为尝试使其正常工作而编写的代码:

配置文件的部分,其中包含与此应用程序相关的所有 log4net 设置:

<appender name="RollingFileAppenderNHibernate"
        type="log4net.Appender.RollingFileAppender">
    <file value="C:\temp\RollingLogFileNHibernate" />
    <appendToFile value="true" />
    <ImmediateFlush value="true" />
    <rollingStyle value="Date" />
    <DatePattern value="yyyyMMdd.\l\o\g" />
    <StaticLogFileName value="false" />
    <MaxSizeRollBackups value="1" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>
<logger name="NHibernate.SQL"
        additivity="false">
    <level value="ALL"/>
    <appender-ref ref="RollingFileAppenderNHibernate"/>
</logger>

我的 NHibernate 配置:

public static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
        .Database(OracleClientConfiguration.Oracle10
        .ConnectionString((conn =>
            conn.FromConnectionStringWithKey("APPDB")))
        .ShowSql())
        .Mappings(m =>
            m.FluentMappings.AddFromAssemblyOf<{ObjectName}>())
        .BuildSessionFactory();
}

在我的 Global.asax.cs 的 Application_Start() 中:

log4net.Config.XmlConfigurator.Configure();

我尝试了这些设置的几种不同变体,但结果总是相同的,一个空的日志文件。

I'm trying to get logging enabled on my application using Fluent NHibernate and log4net. I have tried the things described here, here, here and here. The log file is getting created, but nothing is getting written to it. The other log files for this and other applications all seam to be working OK, so I'm assuming the issue is something with my configuration.

Here is the code I have put in place to try to get this working:

The section of the config file with all of my log4net settings relevant to this APP:

<appender name="RollingFileAppenderNHibernate"
        type="log4net.Appender.RollingFileAppender">
    <file value="C:\temp\RollingLogFileNHibernate" />
    <appendToFile value="true" />
    <ImmediateFlush value="true" />
    <rollingStyle value="Date" />
    <DatePattern value="yyyyMMdd.\l\o\g" />
    <StaticLogFileName value="false" />
    <MaxSizeRollBackups value="1" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>
<logger name="NHibernate.SQL"
        additivity="false">
    <level value="ALL"/>
    <appender-ref ref="RollingFileAppenderNHibernate"/>
</logger>

My NHibernate configuration:

public static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
        .Database(OracleClientConfiguration.Oracle10
        .ConnectionString((conn =>
            conn.FromConnectionStringWithKey("APPDB")))
        .ShowSql())
        .Mappings(m =>
            m.FluentMappings.AddFromAssemblyOf<{ObjectName}>())
        .BuildSessionFactory();
}

In the Application_Start() of my Global.asax.cs:

log4net.Config.XmlConfigurator.Configure();

I have tried several different variations of these settings but the result is always the same, an empty log file.

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

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

发布评论

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

评论(2

〃安静 2024-08-16 21:59:01

如果 XML 是您的配置的真实代表,我可以看到两个潜在的问题:

  1. 没有“>”在附加器开始标记的末尾。
  2. 有>在布局结束标记的末尾而不是“>”。

当然,这些可能只是复制粘贴到这篇文章造成的。

If the XML is a true repesentation of your config, I can see two potential problems:

  1. There is no '>' at the end of the appender opening tag.
  2. There is > at the end of the layout closing tag instead of '>'.

Of course, these may have just been caused by the copy and paste into this post.

岁吢 2024-08-16 21:59:01

事实证明,问题与我们围绕 log4net 的框架代码中处理日志记录的方式过于复杂和复杂有关。一旦我找到了跳过日志的正确步骤,日志就开始正常工作了。

It turns out that the problem had to do with the overly complicated and convoluted way that logging was handled in our framework code that we have wrapped around log4net. Once I figured out the correct hoops to jump through the logging started working correctly.

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