Can I use the mvc mini profiler in a console application?
Asked Answered
L

3

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.

Lankton answered 11/9, 2011 at 11:15 Comment(0)
L
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

Lambkin answered 10/7, 2012 at 6:8 Comment(3)
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 :)Gather
It seems not to be compatible with release 3.1.1.140. Any plans to fix it?Phallus
@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.140Goodnight
G
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.

Greenfield answered 11/9, 2011 at 11:31 Comment(2)
@Mark... do you have any screenshots of this running in production? Are you maintaining it with the latest builds of MiniProfiler?Thadthaddaus
@RichardB my main focus is on web. I don't have any specific example scenarios in a console.Greenfield
A
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:

Agueda answered 2/8, 2017 at 9:5 Comment(2)
For me MiniProfiler.Settings.ProfilerProvider = new SingletonProfilerProvider(); made the job. This bit is missing in original documentation for COnsoleApp use case on GitHubPacificism
@Pacificism did you have an issue of some kind with default provider?Agueda

© 2022 - 2024 — McMap. All rights reserved.