处理通过 Log4Net 生成的 AdoNetAppender 错误

发布于 2024-11-19 00:51:11 字数 768 浏览 2 评论 0原文

我使用的是 Log4Net,而不是使用 XML 配置文件,而是完全用代码构建 fileAppender 和 AdoNetAppender。当 adoNetAppender 无法连接到指定的数据库时,它会消耗错误并将错误写入控制台窗口,而不是抛出错误。这让我无法抓住。最终我想捕获配置错误,然后加载 fileAppender,以防数据库关闭。

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);
}

由于消耗了此错误,它永远不会命中此 catch 语句。

有什么建议如何处理吗?

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?

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

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

发布评论

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

评论(1

九局 2024-11-26 00:51:11

也许可以考虑设置 ReConnectOnError 属性(连接字符串中的连接超时非常短):

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

Log4net 是非常专门设计的,因此它永远不会出现异常。我想您可以修改附加程序或从中派生来更改此行为。

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.

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