0

如果 app.config 格式错误,例如不是格式正确的 XML 文件,应用程序将无法加载。是否有任何方法可以让我知道此类问题 - 例如,如果 app.config 由于格式错误的 XML 文件而加载错误,则接收一些事件(以便我可以编写文件日志和事件日志来记录此问题)?

提前谢谢,乔治

我的代码和 app.config 看起来像这样,但没有抛出异常。

    class Program
    {
        public static void MyEventHandler(object sender, EventArgs e)
        {
            return;
        }

        static void Main(string[] args)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += MyEventHandler;

            return;
        }
    }

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configuration>
4

1 回答 1

2

在您的应用程序启动中,例如在主类的静态构造函数中。您可以将其定义为

  AppDomain currentDomain = AppDomain.CurrentDomain;
  currentDomain.UnhandledException += MyHandler; // define MyHanlder somewhere.

捕获由于配置引起的 ConfigurationErrorsException。

于 2009-02-27T12:20:55.630 回答