开发者

Handling AdoNetAppender Errors generated via Log4Net

开发者 https://www.devze.com 2023-03-18 19:29 出处:网络
I\'m using Log4Net and instead of using an XML configuration file, I\'m building both the fileAppender, and the AdoNetAppender completely in code. When the adoNetAppender is unable to connect to the s

I'm using Log4Net and instead of using an XML configuration file, I'm building both the fileAppender, and the AdoNetAppender completely in code. When the adoNetAppender is unable to connect to the specified database, it consumes and writes the error to the console window instead of throwing it. This makes it impossible for me to catch. Ultimately I would like to catch the configuration error, and then load the fileAppender incase the DB is down.

try {
    log4net.Repository.Hierarchy.Hierarchy hier = log4net.LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy;

    if (hier != null)
    {

        log4net.Appender.AdoNetAppender adoAppender = new log4net.Appender.AdoNetAppender();
        adoAppender.Name = "AdoNetAppender";
        // more code..
}
catch (Exception ex)
{
    LoadFileAppender(level);
    Log log = new Log("AdoAdaptorFail");
    log.Error("AdoAdaptor Failed", "System", ex);
}

It never hits this catch statement because开发者_运维百科 of the consuming of this error.

Any advice on how to handle?


Maybe consider setting the ReConnectOnError property (with a very short connect time out in the connection string):

http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.ReconnectOnError.html

Log4net is very specifically made so that it will never through an exception. I guess you could either modify the appender or derive from it to change this behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消