using BQoolCommon.Helpers.Model; using Elmah; using ResearchManager.Web.App_Start; using System; using System.Configuration; using System.Web; using System.Web.Mvc; using System.Web.Routing; using StackExchange.Profiling; using StackExchange.Profiling.Mvc; using StackExchange.Profiling.EntityFramework6; using System.Web.Optimization; using NLog; using ResearchManager.Models.ValidateAttribute; namespace ResearchManager.Web { public class MvcApplication : HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //Remove Header X-AspNetMvc-Version MvcHandler.DisableMvcResponseHeader = true; AutofacConfig.Bootstrapper(); //堆代码 duidaima.com ModelBinders.Binders.DefaultBinder = new BQoolModelBinder(); //註冊自訂的 Validation (複寫預設的錯誤訊息) CustomerValidation.RegisterCustomerValidation(); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CustomizedRequired), typeof(RequiredAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CustomizedStringLength), typeof(StringLengthAttributeAdapter)); #region 测试 //////if (bool.Parse(ConfigurationManager.AppSettings["MiniProfiler"] ?? "false")) //////{ ////MiniProfiler.Configure(new MiniProfilerOptions ////{ //// RouteBasePath = "~/profiler", //// PopupRenderPosition = RenderPosition.Right, // defaults to left //// PopupMaxTracesToShow = 10, // defaults to 15 //// ResultsAuthorize = request => request.IsLocal, //// ResultsListAuthorize = request => //// { //// return true; // all requests are legit in this example //// }, //// // Stack trace settings //// StackMaxLength = 256, // default is 120 characters //// TrackConnectionOpenClose = true ////} //// .ExcludeType("SessionFactory") // Ignore any class with the name of SessionFactory) //// .ExcludeAssembly("NHibernate") // Ignore any assembly named NHibernate //// .ExcludeMethod("Flush") // Ignore any method with the name of Flush //// .AddViewProfiling() // Add MVC view profiling (you want this) //// ); ////MiniProfilerEF6.Initialize(); //////} #endregion 测试 } protected void Application_BeginRequest(Object source, EventArgs e) { BundleTable.EnableOptimizations = false; #region 测试 ////MiniProfiler.StartNew(); #endregion 测试 } protected void Application_EndRequest() { #region 测试 //////MiniProfiler.Current?.Stop(); // Be sure to stop the profiler! #endregion 测试 } #region Elmah private static readonly string _exceptionMsg = "A potentially dangerous Request.Path value was detected from the client"; protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); Logger nlogger = LogManager.GetCurrentClassLogger(); nlogger.Error(ex); if (BQoolCommon.Helpers.Setting.CommonSetting.IsProd()) { if (e is ExceptionFilterEventArgs exceptionFilter) { if (exceptionFilter.Exception is HttpException httpException && httpException.Message.StartsWith(_exceptionMsg)) { Response.Redirect("/"); } } Response.Clear(); Server.ClearError(); Response.StatusCode = 404; } } /// <summary> /// 排除 Elmah 404 寄信通知 /// </summary> public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e) { if (e.Exception is HttpException httpException && (httpException.GetHttpCode() == 404 || httpException.Message.StartsWith(_exceptionMsg))) { e.Dismiss(); } } /// <summary> /// 自定 Elmah 發信主旨 /// </summary> private void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e) { string machineName = "none server"; try { if (Request != null) { machineName = Request.ServerVariables["HTTP_HOST"]; } } catch { } // 取得 Elamh ErrorMail 的主旨 // "$MachineName$ at $ErrorTime$ : {0}" string elmahSubject = e.Mail.Subject; //替換 ErrorMail 的主旨內容 string emailSubject = string.Format("ResearchManager.Web Error => {0}", elmahSubject .Replace("$MachineName$", machineName) ); e.Mail.Subject = emailSubject; } #endregion Elmah } }运行效果
运行项目,http://localhost//profiler/results-index 即可看到监测结果