22

How can I use the mvc-mini-profiler in a C# console application?

Also curious, does the mvc mini profiler put the actual profiled statistics in the left side of each web page or is that just custom javascript code done manually?

I downloaded and ran the mvc sample, and can't find that code that puts the results in that javascript popup.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

20

yes you can! And I have created a NuGet package to make it even easier to get it running.

Check out MiniProfiler.Windows: http://nootn.github.com/MiniProfiler.Windows/

Also for a detailed article on how to design a good console app for profiling: http://healthedev.blogspot.com.au/2012/07/performance-profiling-console-or-winwpf.html

nootn
  • 851
  • 1
  • 12
  • 16
  • 2
    Hey, your nuget package works great. One small thing: I had to use an assembly redirect as it appears to depend on an earlier version of MiniProfiler than the latest. Consider updating your nuspec to depend on the latest version. Thanks for the effort anyway :) – Paul Suart Mar 05 '13 at 20:37
  • 2
    It seems not to be compatible with release 3.1.1.140. Any plans to fix it? – Darek Nov 22 '14 at 22:06
  • 2
    @PaulSuart I am facing the followint error `Method 'Start' in type 'MiniProfiler.Windows.ConsoleProfilingProvider' from assembly 'MiniProfiler.Windows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation." (System.TypeLoadException) Exception Message = "Method 'Start' in type 'MiniProfiler.Windows.ConsoleProfilingProvider' from assembly 'MiniProfiler.Windows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.` How to update nuspec ? There is no .nuspec file in my solution folder. MiniProfiler version is 3.1.1.140 – Abdul Rauf Feb 02 '15 at 07:05
11

The core timing object (MiniProfiler) should work OK, as should the profiled-connection hooks etc; however you would need to;

  • add your own UI to show the results
  • define your own scope / lifetime (for web it is easy - just the http-request)
  • provide your own storage hooks (there is an extension API for this, that uses http-context by default IIRC)
  • either pass the profiler around manually, or define a sensible way to look up the current profiler instance

I know some people have used portions of it for WPF etc, so it can be used - but IMO it might make sense to pick just the bits that make sense, and use those to write a custom library that adds some awesome.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    @Mark... do you have any screenshots of this running in production? Are you maintaining it with the latest builds of MiniProfiler? – Richard B Feb 04 '13 at 20:04
  • @RichardB my main focus is on web. I don't have any specific example scenarios in a console. – Marc Gravell Feb 04 '13 at 20:07
3

MiniProfiler V4 (currently prerelease) has method RenderPlainText(). You can use it directly in console or multithreaded asynchronous server application without any additional setup:

public void Foo()
{
    MiniProfiler.Start("Interesting subroutine");
    using (MiniProfiler.Current.Step("Step1"))
    {
        using (MiniProfiler.Current.Step(nameof(AccessDb)))
        {
            AccessDb();
        }
        Thread.Sleep(100);
    }
    using (MiniProfiler.Current.Step("Step2"))
    {
        Thread.Sleep(100);
    }
    MiniProfiler.Stop();
    Console.WriteLine(MiniProfiler.Current.RenderPlainText());
}

This code snippet produces following output:

PCName at 8/2/2017 8:44:36 AM
  Interesting subroutine = 309.2ms
> Step1 = 204.8ms
>> AccessDb = 103.8ms (sql = 56.2ms in 2 cmds)
> Step2 = 100.9ms

As you can see output includes short summary of custom timings (sql in this case). You can easily alter this behavior (for example, to include command text) by providing your own version of RenderPlainText().

For more information check:

Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
  • 1
    For me `MiniProfiler.Settings.ProfilerProvider = new SingletonProfilerProvider();` made the job. This bit is missing in original documentation for COnsoleApp use case on GitHub – sll May 03 '18 at 18:42
  • @sll did you have an issue of some kind with default provider? – Leonid Vasilev May 08 '18 at 19:23