无法使用 Miniprofiler 打印查询
Unable to print query using Miniprofiler
我已经在我的项目中集成了 Entity Framework 和 CodeFirstStoredProc 库。我想记录两个库执行的查询。以前我使用的是 EF 提供的 Database.Log 委托,但由于我也想记录来自其他库的查询,我决定为此集成 Miniprofiler。
我使用下面的代码在 result
变量中获取查询日志:
MiniProfilerEF6.Initialize();
MiniProfiler.StartNew("Test");
using (MiniProfiler.Current.Step("Level 1"))
{
DbConnection spConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
ProfiledDbConnection profileSpConnection = new ProfiledDbConnection(spConnection, MiniProfiler.Current);
using (EfDataContext db = new EfDataContext(profileSpConnection))
{
List<Domain.PersonEntity> data = db.Persons.ToList();
}
using (StoredProcedureContext db = new StoredProcedureContext(profileSpConnection))
{
List<GetPersonResult> data = db.GetPerson.CallStoredProc(new Domain.GetPersonParameter { IsActive = true }).ToList<GetPersonResult>();
}
string result = MiniProfiler.Current.RenderPlainText();
}
MiniProfiler.Current.Stop();
我期待包含所有详细信息的输出查询,但不幸的是我得到的结果如下:
Manprit-PC at 11/15/2018 2:24:27 PM
Test = ms
> Level 1 = ms (sql = 45ms in 12 cmds)
我是否缺少实施方面的内容?
分析 EntityFramework 6 时,您需要在第一个查询 之前 挂钩。因此 .Initialize()
调用需要更早发生,当您的应用程序启动时。你可以找到 the MiniProfiler EF6 docs here
根据这些标签,您似乎在使用网络应用程序,所以它应该像这样尽早发生:
using StackExchange.Profiling.EntityFramework6;
protected void Application_Start()
{
MiniProfilerEF6.Initialize();
}
这就是当前版本的 MiniProfilerExtensions.RenderPlainText()
呈现自定义计时信息的方式。自定义计时是使用 CustomTiming()
而不是 Step()
创建的,它们通常是 MiniProfiler 层次结构中的叶度量,例如数据库交互或 HTTP 请求。
您可以轻松自定义渲染过程并渲染有关自定义计时的详细信息:
SERVER at 23.11.2018 09:00:00
MiniProfiler = 48,3[ms]
> Foo = 35,6ms (sql = 24,8[ms] in 1 cmd) (http = 4,7[ms] in 1 cmd)
sql 24,8[ms] +16,9[ms] SELECT * FROM Foo
http 4,7[ms] +41,8[ms] GET http://foo.bar
示例实现:
using StackExchange.Profiling;
using StackExchange.Profiling.Internal;
...
public static string CustomRenderPlainText(this MiniProfiler profiler, bool htmlEncode = false)
{
if (profiler == null) return string.Empty;
var text = StringBuilderCache.Get()
.Append(htmlEncode ? WebUtility.HtmlEncode(Environment.MachineName) : Environment.MachineName)
.Append(" at ")
.Append(DateTime.UtcNow)
.AppendLine();
var timings = new Stack<Timing>();
timings.Push(profiler.Root);
while (timings.Count > 0)
{
var timing = timings.Pop();
text.AppendFormat("{0} {1} = {2:###,##0.##}[ms]",
new string('>', timing.Depth),
htmlEncode ? WebUtility.HtmlEncode(timing.Name) : timing.Name,
timing.DurationMilliseconds);
if (timing.HasCustomTimings)
{
// TODO: Customize this code block.
// Custom timings grouped by category. Collect all custom timings in a list.
var customTimingsFlat = new List<KeyValuePair<string, CustomTiming>>(capacity: timing.CustomTimings.Sum(ct => ct.Value.Count));
foreach (var pair in timing.CustomTimings)
{
var type = pair.Key;
var customTimings = pair.Value;
customTimingsFlat.AddRange(pair.Value.Select(ct => KeyValuePair.Create(type, ct)));
text.AppendFormat(" ({0} = {1:###,##0.##}[ms] in {2} cmd{3})",
type,
customTimings.Sum(ct => ct.DurationMilliseconds),
customTimings.Count,
customTimings.Count == 1 ? string.Empty : "s");
}
foreach (var pair in customTimingsFlat.OrderBy(kvp => kvp.Value.StartMilliseconds))
{
var type = pair.Key;
var ct = pair.Value;
text.AppendLine();
var mainPart = string.Format("{0}{1} {2:###,##0.##}[ms] +{3:###,##0.##}[ms] ",
new string(' ', timing.Depth + 2),
type,
ct.DurationMilliseconds,
ct.StartMilliseconds);
text.Append(mainPart);
// Shift command text to closer to the command for better readability.
var prefix = new string(' ', mainPart.Length);
string cmdLine = null;
using (var reader = new StringReader(ct.CommandString))
{
while ((cmdLine = reader.ReadLine()) != null)
{
text.Append(cmdLine);
if (reader.Peek() == -1 && profiler.Options.ExcludeStackTraceSnippetFromCustomTimings)
{
break;
}
text.AppendLine();
text.Append(prefix);
}
}
if (profiler.Options.ExcludeStackTraceSnippetFromCustomTimings)
{
continue;
}
text.Append(ct.StackTraceSnippet);
}
}
text.AppendLine();
if (timing.HasChildren)
{
var children = timing.Children;
for (var i = children.Count - 1; i >= 0; i--) timings.Push(children[i]);
}
}
return text.ToStringRecycle();
}
另请注意,默认情况下,为了呈现包含所有时间的 MiniProfiler 报告,您需要先调用 Stop()
。您也可以通过在报告中计算到目前为止的时间来自定义它。
我已经在我的项目中集成了 Entity Framework 和 CodeFirstStoredProc 库。我想记录两个库执行的查询。以前我使用的是 EF 提供的 Database.Log 委托,但由于我也想记录来自其他库的查询,我决定为此集成 Miniprofiler。
我使用下面的代码在 result
变量中获取查询日志:
MiniProfilerEF6.Initialize();
MiniProfiler.StartNew("Test");
using (MiniProfiler.Current.Step("Level 1"))
{
DbConnection spConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
ProfiledDbConnection profileSpConnection = new ProfiledDbConnection(spConnection, MiniProfiler.Current);
using (EfDataContext db = new EfDataContext(profileSpConnection))
{
List<Domain.PersonEntity> data = db.Persons.ToList();
}
using (StoredProcedureContext db = new StoredProcedureContext(profileSpConnection))
{
List<GetPersonResult> data = db.GetPerson.CallStoredProc(new Domain.GetPersonParameter { IsActive = true }).ToList<GetPersonResult>();
}
string result = MiniProfiler.Current.RenderPlainText();
}
MiniProfiler.Current.Stop();
我期待包含所有详细信息的输出查询,但不幸的是我得到的结果如下:
Manprit-PC at 11/15/2018 2:24:27 PM
Test = ms
> Level 1 = ms (sql = 45ms in 12 cmds)
我是否缺少实施方面的内容?
分析 EntityFramework 6 时,您需要在第一个查询 之前 挂钩。因此 .Initialize()
调用需要更早发生,当您的应用程序启动时。你可以找到 the MiniProfiler EF6 docs here
根据这些标签,您似乎在使用网络应用程序,所以它应该像这样尽早发生:
using StackExchange.Profiling.EntityFramework6;
protected void Application_Start()
{
MiniProfilerEF6.Initialize();
}
这就是当前版本的 MiniProfilerExtensions.RenderPlainText()
呈现自定义计时信息的方式。自定义计时是使用 CustomTiming()
而不是 Step()
创建的,它们通常是 MiniProfiler 层次结构中的叶度量,例如数据库交互或 HTTP 请求。
您可以轻松自定义渲染过程并渲染有关自定义计时的详细信息:
SERVER at 23.11.2018 09:00:00
MiniProfiler = 48,3[ms]
> Foo = 35,6ms (sql = 24,8[ms] in 1 cmd) (http = 4,7[ms] in 1 cmd)
sql 24,8[ms] +16,9[ms] SELECT * FROM Foo
http 4,7[ms] +41,8[ms] GET http://foo.bar
示例实现:
using StackExchange.Profiling;
using StackExchange.Profiling.Internal;
...
public static string CustomRenderPlainText(this MiniProfiler profiler, bool htmlEncode = false)
{
if (profiler == null) return string.Empty;
var text = StringBuilderCache.Get()
.Append(htmlEncode ? WebUtility.HtmlEncode(Environment.MachineName) : Environment.MachineName)
.Append(" at ")
.Append(DateTime.UtcNow)
.AppendLine();
var timings = new Stack<Timing>();
timings.Push(profiler.Root);
while (timings.Count > 0)
{
var timing = timings.Pop();
text.AppendFormat("{0} {1} = {2:###,##0.##}[ms]",
new string('>', timing.Depth),
htmlEncode ? WebUtility.HtmlEncode(timing.Name) : timing.Name,
timing.DurationMilliseconds);
if (timing.HasCustomTimings)
{
// TODO: Customize this code block.
// Custom timings grouped by category. Collect all custom timings in a list.
var customTimingsFlat = new List<KeyValuePair<string, CustomTiming>>(capacity: timing.CustomTimings.Sum(ct => ct.Value.Count));
foreach (var pair in timing.CustomTimings)
{
var type = pair.Key;
var customTimings = pair.Value;
customTimingsFlat.AddRange(pair.Value.Select(ct => KeyValuePair.Create(type, ct)));
text.AppendFormat(" ({0} = {1:###,##0.##}[ms] in {2} cmd{3})",
type,
customTimings.Sum(ct => ct.DurationMilliseconds),
customTimings.Count,
customTimings.Count == 1 ? string.Empty : "s");
}
foreach (var pair in customTimingsFlat.OrderBy(kvp => kvp.Value.StartMilliseconds))
{
var type = pair.Key;
var ct = pair.Value;
text.AppendLine();
var mainPart = string.Format("{0}{1} {2:###,##0.##}[ms] +{3:###,##0.##}[ms] ",
new string(' ', timing.Depth + 2),
type,
ct.DurationMilliseconds,
ct.StartMilliseconds);
text.Append(mainPart);
// Shift command text to closer to the command for better readability.
var prefix = new string(' ', mainPart.Length);
string cmdLine = null;
using (var reader = new StringReader(ct.CommandString))
{
while ((cmdLine = reader.ReadLine()) != null)
{
text.Append(cmdLine);
if (reader.Peek() == -1 && profiler.Options.ExcludeStackTraceSnippetFromCustomTimings)
{
break;
}
text.AppendLine();
text.Append(prefix);
}
}
if (profiler.Options.ExcludeStackTraceSnippetFromCustomTimings)
{
continue;
}
text.Append(ct.StackTraceSnippet);
}
}
text.AppendLine();
if (timing.HasChildren)
{
var children = timing.Children;
for (var i = children.Count - 1; i >= 0; i--) timings.Push(children[i]);
}
}
return text.ToStringRecycle();
}
另请注意,默认情况下,为了呈现包含所有时间的 MiniProfiler 报告,您需要先调用 Stop()
。您也可以通过在报告中计算到目前为止的时间来自定义它。