简体   繁体   English

使用log4net启用日志记录!

[英]Enable logging using log4net!

Is it possible to log to a database and a log file from the same program using log4net? 是否可以使用log4net从同一程序登录数据库和日志文件?

I want certain sections of my code to log to a DB instead of a log file. 我希望代码的某些部分登录到数据库而不是日志文件。 Everything else can continue to log as before. 其他所有内容都可以像以前一样继续记录。

In the log4net configuration you just need to add another appender and its done. 在log4net配置中,您只需要添加另一个追加器并完成。 Have a look here on how to configure appenders. 在这里了解如何配置附加程序。

Lets have a config example ( stolen from the docs ) 让我们有一个配置示例(从文档中窃取)

    <log4net>
        <!-- A1 is set to be a ConsoleAppender -->
        <appender name="A1" type="log4net.Appender.ConsoleAppender">

            <!-- A1 uses PatternLayout -->
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
            </layout>
        </appender>


        <root>
            <level value="DEBUG" />
            <appender-ref ref="A1" />
<!-- as many appenders as you want -->
            <appender-ref ref="...." />
            <appender-ref ref="...." />
        </root>
    </log4net>

As Felice said, just add another appender in your configuration file, here's an example of a config-file with multiple appenders: 正如Felice所说,只需在配置文件中添加另一个附加程序,这是一个带有多个附加程序的配置文件示例:

http://pastebin.ca/2048432 http://pastebin.ca/2048432

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM