Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Miniprofiler打印查询_C#_.net_Mvc Mini Profiler_Miniprofiler - Fatal编程技术网

C# 无法使用Miniprofiler打印查询

C# 无法使用Miniprofiler打印查询,c#,.net,mvc-mini-profiler,miniprofiler,C#,.net,Mvc Mini Profiler,Miniprofiler,我在我的项目中集成了实体框架和CodeFirstStoredProc库。我想记录两个库执行的查询。以前我使用的是EF提供的Database.Log委托,但由于我还想记录来自其他库的查询,所以我决定集成Miniprofiler 我使用以下代码在result变量中获取查询日志: MiniProfilerEF6.Initialize(); MiniProfiler.StartNew("Test"); using (MiniProfiler.Current.Step("L

我在我的项目中集成了实体框架和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();

我是否缺少实现方面的内容?

在分析EntityFramework 6时,需要在第一次查询之前进行连接。因此,
.Initialize()
调用需要在应用程序启动时更早地进行。你可以找到

给定这些标记,看起来您处于web应用程序中,因此应该像这样提前发生:

using StackExchange.Profiling.EntityFramework6;

protected void Application_Start()
{
    MiniProfilerEF6.Initialize();
}

这就是当前版本呈现自定义计时信息的方式。自定义计时是使用
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();
}
使用StackExchange.Profiling;
使用StackExchange.Profiling.Internal;
...
公共静态字符串CustomRenderPlainText(此MiniProfiler分析器,bool htmlEncode=false)
{
if(profiler==null)返回string.Empty;
var text=StringBuilderCache.Get()
.Append(htmlEncode?WebUtility.htmlEncode(Environment.MachineName):Environment.MachineName)
.Append(“at”)
.Append(DateTime.UtcNow)
.AppendLine();
变量计时=新堆栈();
推送(profiler.Root);
同时(timings.Count>0)
{
var timing=timings.Pop();
text.AppendFormat(“{0}{1}={2:##########0.###}[ms]”,
新字符串('>',timening.Depth),
htmlEncode?WebUtility.htmlEncode(timing.Name):timing.Name,
计时(持续时间毫秒);
if(timing.HasCustomTimings)
{
//TODO:自定义此代码块。
//按类别分组的自定义计时。在列表中收集所有自定义计时。
var customTimingsFlat=新列表(容量:timing.CustomTimings.Sum(ct=>ct.Value.Count));
foreach(定时中的var对。自定义定时)
{
var type=pair.Key;
var customTimings=pair.Value;
customTimingsFlat.AddRange(pair.Value.Select(ct=>KeyValuePair.Create(type,ct));
text.AppendFormat({0}={1:######,###0.###}[ms]在{2}cmd{3}中),
类型,
customTimings.Sum(ct=>ct.Duration毫秒),
自定义计时。计数,
customTimings.Count==1?字符串。空:“s”);
}
foreach(customTimingsFlat.OrderBy中的var对(kvp=>kvp.Value.StartMillSeconds))
{
var type=pair.Key;
var ct=配对值;
text.AppendLine();
var mainPart=string.Format(“{0}{1}{2:##########0.####[ms]+{3:#####0.#####[ms]”,
新字符串(“”,计时深度+2),
类型,
ct.duration毫秒,
ct.开始(毫秒);
text.Append(主要部分);
//将命令文本移动到更靠近命令的位置,以提高可读性。
变量前缀=新字符串(“”,mainPart.Length);
字符串cmdLine=null;
使用(var reader=newstringreader(ct.CommandString))
{
而((cmdLine=reader.ReadLine())!=null)
{
text.Append(cmdLine);
if(reader.Peek()=-1&&profiler.Options.ExcludeStackTraceSnippetFromCustomTimeings)
{
打破
}
text.AppendLine();
text.Append(前缀);
}
}
if(profiler.Options.ExcludeStackTraceSnippetFromCustomTimeings)
{
继续;
}
text.Append(ct.StackTraceSnippet);
}
}
text.AppendLine();
if(计时。有子项)
{
var children=计时。children;
对于(vari=children.Count-1;i>=0;i--)计时。推(children[i]);
}
}
返回text.toString循环();
}

还请注意,默认情况下,为了呈现具有所有计时的MiniProfiler报告,您需要首先调用
Stop()
。您也可以通过计算报表中到目前为止的计时来自定义此设置

♦: 我正在使用控制台应用程序,我已经添加了
MiniProfilerEF6.Initialize()在第一行。我可能错了,但MiniProfiler的当前实现似乎没有使用实际的SQL命令文本创建自定义计时。“我用的是整洁的。”奥利维耶马特罗说,“当然应该用。”。但是您需要利用
ProfiledDbConnection
decorator。请参阅MiniProfiler文档的第页。我是,但我一定是遗漏了什么,因为IDbProfiler设置为MiniProfiler.Current时,我在调用扩展方法时得到了基本结果。时间是最重要的false@OlivierMATROT没有额外的信息,就不可能说出问题所在。请提出一个包含所有细节的单独问题,我会看一看。
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();
}