有人可以告诉我 log4net 配置/实现有什么问题吗?

发布于 2024-12-19 21:19:52 字数 1552 浏览 7 评论 0原文

以前从未使用过 log4net,但我最终可能会为此自责……如果我能让它工作的话。

我的 app.config 中有以下内容:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

...

<log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="c:\FRLogs\log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender"/>
    </root>
</log4net>

在我的代码中,我有:

protected static readonly ILog Log = LogManager.GetLogger(typeof(FileRouter));

然后有几个调用,例如:

Log.Debug(string.Format("Processing Route {0} for server {1}, source connection {2} ({3})",
            route.RouteID, server.ServerID,
            route.SourceConnection.ConnectionID,
            route.Description));

从我读到的内容来看,我应该在 c:\FRLogs\ 中获得一个不错的日志文件log.txt 包含各种很酷的调试信息,但该文件没有创建,我不知道出了什么问题。我的服务可以写入该目录,因此(对我来说)这似乎不是权限问题。

never used log4net before, but I may end up kicking myself for that... if I can get it working.

I have the following in my app.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

...

<log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="c:\FRLogs\log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender"/>
    </root>
</log4net>

In my code, I have:

protected static readonly ILog Log = LogManager.GetLogger(typeof(FileRouter));

and then several calls like:

Log.Debug(string.Format("Processing Route {0} for server {1}, source connection {2} ({3})",
            route.RouteID, server.ServerID,
            route.SourceConnection.ConnectionID,
            route.Description));

From what I've read, I should be getting a nice log file in c:\FRLogs\log.txt with all sorts of cool debug information, but the file does not get created, and I do not know what is wrong. My service can write to the directory, so it does not seem (to me) to be a permissions thing.

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

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

发布评论

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

评论(1

逐鹿 2024-12-26 21:19:52

除了初始化记录器之外,您已经完成了所有操作。您可以通过几种方法来完成此操作,这里有有用的记录:

log4net 初始化

我们以编程方式亲自初始化它,所以对此也有一篇很棒的文章:

如何从头开始以编程方式配置 log4net(无配置)

You've done everything right except initialize the logger. You can do it a few ways, which are usefully documented here:

log4net initialisation

We programmatically initialize it personally, and SO has a great post on this, too:

How to configure log4net programmatically from scratch (no config)

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