views:

934

answers:

1

Hello everyone,

If the app.config format is wrong, for example, not a correct format XML file, application will fail from loading. Are there any ways to let me know such issue -- for example, receiving some events (so that I could write file log and event log to record this issue) if app.config loads error because of a mal-formatted XML file?

thanks in advance, George

My code and app.config looks like this, but no exception is thrown.

    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>
+2  A: 

Within your application start up, e.g. within a static constructor of the main class. you can define it as

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

to catch the ConfigurationErrorsException due to the config.

codemeit
You should note that when you got an AppDomain.UnhandledException you application WILL be terminated no matter what you do.
Alex Reitbort
Thanks codemeit!I have written code like this, but how could I know it is exception from wrong app.config format? public static void MyEventHandler(object sender, EventArgs e) { return; }
George2
Thanks for your suggestion, Alex! But how could we distinguish from application configuration loading error because wrong XML format between other types of exceptions?
George2
try to see whats in sender and eventArgs?
codemeit
I have posted my code and the wrong format app.config, but no exception is thrown. Any ideas why?
George2