using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Bilfinger.HRcules.SuccessFactorWebFrontend
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private static ILog log;

        protected void Session_Start()
        {
        }


        protected void Application_Start()
        {
            
            string l4net = Server.MapPath(path: "~/log4net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
            log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            log.Info(message: "Starte Anwendung");

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            log.Info(message: "Ende Application_Start");
        }

        void Application_Error(object sender, EventArgs e)
        {
            //ToDo: log funktioniert nicht, da log null ist
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                log.Error(ex.Message, ex);
                if (ex.InnerException != null)
                {
                    log.Error(message: "Inner Exception", exception: ex.InnerException);
                }
            }
            else
            {
                log.Error("Ein unbekannter Fehler ist aufgetreten.");
            }
        }
    }
}
