Parcourir la source

依赖注入stopwatch

懒得勤快 il y a 5 ans
Parent
commit
a1613e53ac

+ 2 - 0
src/Masuit.MyBlogs.Core/Configs/AutofacModule.cs

@@ -2,6 +2,7 @@
 using Hangfire;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Extensions.Hangfire;
+using System.Diagnostics;
 using System.Reflection;
 
 namespace Masuit.MyBlogs.Core.Configs
@@ -14,6 +15,7 @@ namespace Masuit.MyBlogs.Core.Configs
             builder.RegisterType<BackgroundJobClient>().SingleInstance();
             builder.RegisterType<FirewallAttribute>().PropertiesAutowired().AsSelf().InstancePerDependency();
             builder.RegisterType<HangfireBackJob>().As<IHangfireBackJob>().PropertiesAutowired(PropertyWiringOptions.PreserveSetValues).InstancePerDependency();
+            builder.Register(c => new Stopwatch()).As<Stopwatch>().AsSelf().PropertiesAutowired(PropertyWiringOptions.PreserveSetValues).InstancePerLifetimeScope();
         }
     }
 }

+ 2 - 2
src/Masuit.MyBlogs.Core/Controllers/BaseController.cs

@@ -53,7 +53,8 @@ namespace Masuit.MyBlogs.Core.Controllers
 
         public IMapper Mapper { get; set; }
         public MapperConfiguration MapperConfig { get; set; }
-        public Stopwatch Stopwatch { get; } = new Stopwatch();
+        public Stopwatch Stopwatch { get; set; }
+
         /// <summary>
         /// 响应数据
         /// </summary>
@@ -183,7 +184,6 @@ namespace Masuit.MyBlogs.Core.Controllers
         public void OnResultExecuting(ResultExecutingContext context)
         {
             Stopwatch.Restart();
-            ViewData["ResultWatch"] = Stopwatch;
         }
     }
 }

+ 2 - 1
src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml

@@ -5,6 +5,7 @@
 @using Masuit.MyBlogs.Core.Models.ViewModel
 @using Masuit.Tools
 @using Masuit.Tools.Core.Net
+@inject Stopwatch stop
 @{
     string[] colors = { "success", "info", "warning", "danger", "default" };
     IList<MenuOutputDto> menus = ViewBag.menus;
@@ -262,7 +263,7 @@
             <div class="cd-overlay"></div>
             <footer class="footer wow fadeIn">
                 <div class="container">
-                    <span class="counter">总访客量:@CommonHelper.InterviewCount,平均访问量:@(CommonHelper.AverageCount.ToString("N"))人/天,数据耗时:@ViewData["ActionElapsed"],页面耗时:@(((Stopwatch)ViewData["ResultWatch"]).ElapsedMilliseconds)ms</span>
+                    <span class="counter">总访客量:@CommonHelper.InterviewCount,平均访问量:@(CommonHelper.AverageCount.ToString("N"))人/天,数据耗时:@ViewData["ActionElapsed"],页面耗时:@(stop.ElapsedMilliseconds)ms</span>
                     <section>
                         友情链接:
                         @{