简体   繁体   中英

Enable logging using log4net!

Is it possible to log to a database and a log file from the same program using 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. 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:

http://pastebin.ca/2048432

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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