- Unhandled exception handler added - it creates a log message.
- config.backup.xml file created and used in case config.xml is corrupted - LocalSettings.Assign() function avoided - static LocalSettins LocalSettings.Load(string fname) returns a reference to settings
This commit is contained in:
@@ -23,8 +23,9 @@ namespace TBF
|
||||
const string log4netConfigFName = "log4netConfig.xml";
|
||||
|
||||
/// Local settings
|
||||
public static LocalSettings LocalSettings = new LocalSettings();
|
||||
public static LocalSettings LocalSettings;
|
||||
public const string LocalSettingsFileName = "config.xml";
|
||||
public const string LocalSettingsBackupName = "config.backup.xml";
|
||||
|
||||
/// Database related: This object reference is set after a successful user login
|
||||
public static DatabaseSettings CurrentBench;
|
||||
@@ -65,6 +66,8 @@ namespace TBF
|
||||
{
|
||||
log.Fatal("--------------------------------------------------------------------------------");
|
||||
|
||||
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
|
||||
|
||||
/// Local program configuration directory including the trailing backslash
|
||||
string locAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
|
||||
Path.DirectorySeparatorChar + "TestBenchFramework" + Path.DirectorySeparatorChar;
|
||||
@@ -103,9 +106,28 @@ namespace TBF
|
||||
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(locAppDataDir + log4netConfigFName));
|
||||
log.Info("Entering application.");
|
||||
|
||||
///
|
||||
/// Load the local settings
|
||||
if (Program.LocalSettings == null) return; /// Fatal error
|
||||
LocalSettings.Load();
|
||||
///
|
||||
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
||||
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
||||
{
|
||||
/// Loading local seetings from regular config file failed. Use the backup
|
||||
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
||||
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
||||
{
|
||||
return; /// Fatal error
|
||||
}
|
||||
else
|
||||
{
|
||||
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/// Loading local seetings from the regular config file was successful. Update the backup
|
||||
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
||||
}
|
||||
|
||||
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(LocalSettings.Language);
|
||||
|
||||
@@ -115,7 +137,7 @@ namespace TBF
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
|
||||
/// Ensure that there is at least one bench in the configuration
|
||||
while (Program.LocalSettings.testBenches.GetLength(0) == 0)
|
||||
while (Program.LocalSettings.BenchesCount == 0)
|
||||
{
|
||||
log.Warn("No test benches in the local configuration -> display an appropriate dialog.");
|
||||
|
||||
@@ -128,7 +150,8 @@ namespace TBF
|
||||
}
|
||||
|
||||
log.Warn("Loading the local settings again.");
|
||||
LocalSettings.Load();
|
||||
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
||||
if (Program.LocalSettings == null) return; /// Fatal error
|
||||
}
|
||||
|
||||
/// User login
|
||||
@@ -214,7 +237,8 @@ namespace TBF
|
||||
{
|
||||
return; /// Exit program
|
||||
}
|
||||
LocalSettings.Load();
|
||||
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
||||
if (Program.LocalSettings == null) return; /// Fatal error
|
||||
break; /// Retry the loop
|
||||
}
|
||||
}
|
||||
@@ -241,20 +265,46 @@ namespace TBF
|
||||
}
|
||||
}
|
||||
|
||||
/// Open the main application window
|
||||
log.Info("Creating the main window");
|
||||
MainWnd = new MainWnd();
|
||||
log.Info("Opening the main window");
|
||||
Application.Run(MainWnd);
|
||||
|
||||
/// Save local settings
|
||||
log.Info("The main window was closed, saving the local parameters");
|
||||
LocalSettings.Save();
|
||||
try
|
||||
{
|
||||
/// Open the main application window
|
||||
log.Info("Creating the main window");
|
||||
MainWnd = new MainWnd();
|
||||
log.Info("Opening the main window");
|
||||
Application.Run(MainWnd);
|
||||
log.Info("The main window was closed");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogException(log, "Exception in Application.Run(MainWnd)", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
/// Save local settings
|
||||
LocalSettings.Save();
|
||||
}
|
||||
|
||||
/// Close Fluent NHibernate
|
||||
/// Todo
|
||||
|
||||
log.Info("Exiting application.");
|
||||
}
|
||||
}
|
||||
|
||||
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
|
||||
{
|
||||
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
|
||||
}
|
||||
|
||||
static void LogException(ILog log, string description, Exception e)
|
||||
{
|
||||
log.FatalFormat("---------------( {0} )---------------", description);
|
||||
log.FatalFormat("Message : {0}", e.Message);
|
||||
if (e.InnerException != null)
|
||||
{
|
||||
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
|
||||
}
|
||||
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
|
||||
log.Fatal("--------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user