public async Task StopErrorLogging() { int errorCount = 0; Exception lastError = null; void Log(Exception ex) { errorCount++; lastError = ex; } var options = new MiniProfilerBaseOptions() { Storage = new KaboomStorage(), StopwatchProvider = () => new UnitTestStopwatch(), OnInternalError = Log }; var profiler = options.StartProfiler(); AddRecursiveChildren(profiler, 1, 10); Assert.Equal(0, errorCount); profiler.Stop(); Assert.Equal(1, errorCount); Assert.IsType <KaboomStorage.BoomBoom>(lastError); profiler = options.StartProfiler(); AddRecursiveChildren(profiler, 1, 10); Assert.Equal(1, errorCount); await profiler.StopAsync().ConfigureAwait(false); Assert.Equal(2, errorCount); Assert.IsType <KaboomStorage.BoomBoom>(lastError); }
protected BaseTest(ITestOutputHelper output) { Output = output; Options = new MiniProfilerBaseOptions() { StopwatchProvider = () => new UnitTestStopwatch() }; }
public ConsoleProfiler(int logOnlyResultsTakingLongerThanMilliseconds = 0) { _logOnlyResultsTakingLongerThanMilliseconds = logOnlyResultsTakingLongerThanMilliseconds; _options = new MiniProfilerBaseOptions() { Storage = new ConsoleStorage(_logOnlyResultsTakingLongerThanMilliseconds) }; _profiler = new DefaultProfilerProvider().Start(null, _options); }
public override MiniProfiler Start(string profilerName, MiniProfilerBaseOptions options) { var profiler = base.Start(profilerName, options); if (_profilers.TryAdd(profiler.Id, profiler)) { OnProfilerStarted?.Invoke(this, new ProfilerStartedEvent(profiler.Id, profiler)); } return(profiler); }
public void Flush() { _options.Storage.Save(_profiler); _options = new MiniProfilerBaseOptions() { Storage = new ConsoleStorage(_logOnlyResultsTakingLongerThanMilliseconds) }; _profiler = new DefaultProfilerProvider().Start(null, _options); }
/// <summary> /// Starts a new MiniProfiler. /// </summary> /// <remarks> /// <para>This is called when WebProfiler calls MiniProfiler.Start() so, /// - as a result of WebRuntime starting the WebProfiler, and /// - assuming profiling is enabled, on every BeginRequest that should be profiled, /// - except for the very first one which is the boot request.</para> /// </remarks> public override MiniProfiler Start(string profilerName, MiniProfilerBaseOptions options) { var first = Interlocked.Exchange(ref _first, 1) == 0; if (first == false) { return(base.Start(profilerName, options)); } _startupProfiler = new MiniProfiler("StartupProfiler", options); CurrentProfiler = _startupProfiler; return(_startupProfiler); }
internal static MiniProfiler GetComplexProfiler(MiniProfilerBaseOptions options) { var mp = new MiniProfiler("Complex", options); for (var i = 0; i < 50; i++) { using (mp.Step("Step " + i)) { for (var j = 0; j < 50; j++) { using (mp.Step("SubStep " + j)) { for (var k = 0; k < 50; k++) { using (mp.CustomTiming("Custom " + k, "YOLO!")) { } } } } } } return(mp); }
public StackExchange.Profiling.MiniProfiler Start(string profilerName, MiniProfilerBaseOptions options) { return(provider.Start(profilerName, options)); }
/// <summary> /// Starts a new profiling session. /// </summary> /// <param name="profilerName">The name for the started <see cref="MiniProfiler"/>.</param> /// <param name="options">The options to use for this profiler, including all downstream commands.</param> public MiniProfiler Start(string profilerName, MiniProfilerBaseOptions options) => _profiler = new MiniProfiler(profilerName, options);
/// <summary> /// Gets the current formatted and filtered stack trace. /// </summary> /// <param name="options">The options to use for this StackTrace fetch.</param> /// <returns>Space separated list of methods</returns> public static string Get(MiniProfilerBaseOptions options) { if (options.StackMaxLength <= 0) { return string.Empty; } bool ShouldExcludeType(MethodBase method) { var t = method.DeclaringType; while (t != null) { if (options.ExcludedTypes.Contains(t.Name)) return true; t = t.DeclaringType; } return false; } var frames = new StackTrace().GetFrames(); if (frames == null) { return string.Empty; } var sb = StringBuilderCache.Get(); int stackLength = 0, startFrame = frames.Length - 1; for (int i = 0; i < frames.Length; i++) { var method = frames[i].GetMethod(); if (stackLength >= options.StackMaxLength // ASP.NET: no need to continue up the chain || method.Name == "System.Web.HttpApplication.IExecutionStep.Execute" || (method.Module.Name == "Microsoft.AspNetCore.Mvc.Core.dll" && method.DeclaringType.Name == "ObjectMethodExecutor")) { frames[i] = null; startFrame = i < 0 ? 0 : i - 1; break; } else if (ShouldExcludeType(method) || options.ExcludedAssemblies.Contains(method.Module.Assembly.GetName().Name) || options.ExcludedMethods.Contains(method.Name)) { frames[i] = null; } else { stackLength += (stackLength > 0 ? 3 : 0) + method.Name.Length; } } for (var i = startFrame; i >= 0; i--) { var f = frames[i]; if (f != null) { var method = f.GetMethod(); if (sb.Length > 0) { sb.Append(" > "); } sb.Append(method.Name); } } return sb.ToStringRecycle(); }
/// <summary> /// Saves the given <paramref name="options"/> as the global <see cref="DefaultOptions"/> available for use globally. /// These are intended to be used by global/background operations where normal context access isn't available. /// </summary> /// <typeparam name="T">The specific type of <see cref="MiniProfilerBaseOptions"/> to use.</typeparam> /// <param name="options">The options object 44to set for background access.</param> public static T Configure <T>(T options) where T : MiniProfilerBaseOptions { DefaultOptions = options ?? throw new ArgumentNullException(nameof(options)); options.Configure(); // Event handler of sorts return(options); }
public MiniProfiler Start(string profilerName, MiniProfilerBaseOptions options) => CurrentProfiler;