using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Tango.BL;
using Tango.Core;
using Tango.Core.DI;
using Tango.Core.Helpers;
using Tango.Logging;
using Tango.PPC.Common;
using Tango.PPC.Common.EventLogging;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.WatchDog;
using Tango.Settings;
namespace Tango.PPC.UI
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
private WpfGlobalExceptionTrapper exceptionTrapper;
public static String[] StartupArgs { get; private set; }
private LogManager LogManager = LogManager.Default;
///
/// Raises the event.
///
/// A that contains the event data.
protected override void OnStartup(StartupEventArgs e)
{
//If no debugger is attached and the argument -debug was passed launch the debugger.
if (e.Args.Contains("-debug") && !Debugger.IsAttached)
{
Debugger.Launch();
}
StartupArgs = e.Args;
//LogManager.RegisterLogger(new ConsoleLogger("Tango PPC Debug"));
LogManager.RegisterLogger(new FileLogger());
LogManager.RegisterLogger(new VSOutputLogger());
LogManager.Log("Application Started...");
base.OnStartup(e);
exceptionTrapper = new WpfGlobalExceptionTrapper();
exceptionTrapper.Initialize(this);
exceptionTrapper.ApplicationCrashed += ExceptionTrapper_ApplicationCrashed;
LogManager.Categories.Clear();
CoreSettings.DefaultDataSource = new DataSource()
{
Address = "localhost\\SQLEXPRESS",
Catalog = "Tango",
IntegratedSecurity = true,
};
SettingsManager.Default.GetOrCreate();
var settings = SettingsManager.Default.GetOrCreate();
if (settings.LoggingCategories.Count == 0)
{
settings.LoggingCategories.Add(LogCategory.Critical);
settings.LoggingCategories.Add(LogCategory.Error);
settings.LoggingCategories.Add(LogCategory.Info);
settings.LoggingCategories.Add(LogCategory.Warning);
}
settings.Save();
LogManager.Categories.AddRange(settings.LoggingCategories);
}
#region Global Exception Trapping
///
/// Handles the ApplicationCrashed event of the ExceptionTrapper.
///
/// The source of the event.
/// The instance containing the event data.
private void ExceptionTrapper_ApplicationCrashed(object sender, ApplicationCrashedEventArgs e)
{
e.TryRecover = true;
try
{
LogManager.Log(e.Exception, "Application Crashed!");
LogManager.Log("Trying to recover from application crash...");
try
{
if (Application.Current == null)
{
new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
}
}
catch { }
try
{
var eventLogger = TangoIOC.Default.GetInstance();
if (eventLogger != null)
{
eventLogger.Log(e.Exception, "Application Crashed!");
}
}
catch { }
Application.Current.Dispatcher.Invoke(async () =>
{
try
{
LogManager.Log("Trying to notify the user about the crash...");
INotificationProvider notificationProvider = TangoIOC.Default.GetInstance();
if (notificationProvider != null)
{
await notificationProvider.ShowError("An unexpected error has occurred. Use the application logs to diagnose and report the problem.");
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error using the notification provider.");
}
});
}
catch (Exception ex)
{
LogManager.Log(ex, "Error in global exception trapper!");
}
}
#endregion
}
}