PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/Global.asax

http://pigeoncms.googlecode.com/
ASP.NET | 95 lines | 82 code | 13 blank | 0 comment | 6 complexity | c7af1cc59e141f2bd2769b07a5383fbc MD5 | raw file
 1<%@ Application Language="C#" %>
 2<%@ Import Namespace="System.Web.Routing" %>
 3<%@ Import Namespace="System.IO" %>
 4<%@ Import Namespace="PigeonCms" %>
 5<%@ Import Namespace="PigeonCms.Core.Offline" %>
 6<%@ Import Namespace="System.Reflection" %>
 7
 8<script runat="server">
 9    
10    void Application_Start(object sender, EventArgs e) 
11    {
12        try
13        {
14            PigeonCms.AppSettingsManager.RefreshApplicationVars();
15            new PigeonCms.MvcRoutesManager().SetAppRoutes();
16            OfflineProvider.ResetOfflineStatus();
17            fixAppDomainRestartWhenTouchingFiles();
18        }
19        catch { }
20        finally { }
21    }
22    
23    void Application_End(object sender, EventArgs e) 
24    {}
25
26    void Application_OnBeginRequest(object sender, EventArgs e)
27    {}   
28        
29    void Application_Error(object sender, EventArgs e) 
30    { 
31        //untrapped errors handler
32        Exception objErr = Server.GetLastError();
33        string url = "";
34        
35        HttpContext c = HttpContext.Current;
36        if (c != null)
37        {
38            url = c.Request.Url.ToString();
39        }
40        
41        string err = Environment.NewLine
42            + "untrapped error occured " + DateTime.Now.ToString() + Environment.NewLine
43            + "Error in: " + url + Environment.NewLine
44            + "Error message: " + objErr.Message.ToString() + Environment.NewLine
45            + "Stack trace: " + Server.GetLastError().ToString() + Environment.NewLine;
46        //Server.ClearError();
47
48        string logFileName = "~/Logs/errors.txt";
49        logFileName = HttpContext.Current.Request.MapPath(logFileName);
50
51        try
52        {
53            //append log only if file exist; 
54            //manually delete file to disable log
55            //manually create file to enable log            
56            if (File.Exists(logFileName))
57            {
58                TextWriter tw = new StreamWriter(logFileName, true);
59                tw.WriteLine(err);
60                tw.Close();
61            }
62        }
63        catch (DirectoryNotFoundException)
64        {
65            Directory.CreateDirectory(
66                HttpContext.Current.Request.MapPath("~/Logs"));
67        }
68        catch (Exception) { }
69        //System.Diagnostics.EventLog.WriteEntry("PigeonCms.Application_Error", err, System.Diagnostics.EventLogEntryType.Error);
70    }
71
72    void Session_Start(object sender, EventArgs e) 
73    {}
74
75    void Session_End(object sender, EventArgs e) 
76    {}
77
78    private void fixAppDomainRestartWhenTouchingFiles()
79    {
80        
81        if (true /*CurrentTrustLevel == AspNetHostingPermissionLevel.Unrestricted*/)
82        {
83            // From: http://forums.asp.net/p/1310976/2581558.aspx
84            // FIX disable AppDomain restart when deleting subdirectory
85            // This code will turn off monitoring from the root website directory.
86            // Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy.
87            PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
88            object o = p.GetValue(null, null);
89            FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
90            object monitor = f.GetValue(o);
91            MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
92            m.Invoke(monitor, new object[] { });
93        }
94    }
95</script>