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