forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfilingViewEngine.cs
More file actions
111 lines (95 loc) · 3.46 KB
/
Copy pathProfilingViewEngine.cs
File metadata and controls
111 lines (95 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using ServiceStack.MiniProfiler;
namespace ServiceStack.Mvc.MiniProfiler
{
/// <summary>
/// You can wrap your view engines with this view to enable profiling on views and partial
/// </summary>
public class ProfilingViewEngine : IViewEngine
{
class WrappedView : IView
{
IView wrapped;
string name;
bool isPartial;
public WrappedView(IView wrapped, string name, bool isPartial)
{
this.wrapped = wrapped;
this.name = name;
this.isPartial = isPartial;
}
public void Render(ViewContext viewContext, System.IO.TextWriter writer)
{
using (Profiler.Current.Step("Render " + (isPartial ? "partial" : "") + ": " + name))
{
wrapped.Render(viewContext, writer);
}
}
}
IViewEngine wrapped;
/// <summary>
/// Wrap your view engines with this to allow profiling
/// </summary>
/// <param name="wrapped"></param>
public ProfilingViewEngine(IViewEngine wrapped)
{
this.wrapped = wrapped;
}
private ViewEngineResult Find(ControllerContext controllerContext, string name, Func<ViewEngineResult> finder, bool isPartial)
{
var profiler = Profiler.Current;
IDisposable block = null;
var key = "find-view-or-partial";
if (profiler != null)
{
block = HttpContext.Current.Items[key] as IDisposable;
if (block == null)
{
HttpContext.Current.Items[key] = block = profiler.Step("Find: " + name);
}
}
var found = finder();
if (found != null && found.View != null)
{
found = new ViewEngineResult(new WrappedView(found.View, name, isPartial: isPartial), this);
if (found != null && block != null)
{
block.Dispose();
HttpContext.Current.Items[key] = null;
}
}
if (found == null && block != null && this == ViewEngines.Engines.Last())
{
block.Dispose();
HttpContext.Current.Items[key] = null;
}
return found;
}
/// <summary>
/// Find a partial
/// </summary>
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
return Find(controllerContext, partialViewName, () => wrapped.FindPartialView(controllerContext, partialViewName, useCache), isPartial: true);
}
/// <summary>
/// Find a view
/// </summary>
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
return Find(controllerContext, viewName, () => wrapped.FindView(controllerContext, viewName, masterName, useCache), isPartial: false);
}
/// <summary>
/// Find a partial
/// </summary>
public void ReleaseView(ControllerContext controllerContext, IView view)
{
wrapped.ReleaseView(controllerContext, view);
}
}
}