Beispiel #1
0
        /// <summary>
        /// Configures a log to be generated based on the elapsed execution time of the code region. The log is generated at
        /// the specified log level using a custom template.
        /// <para></para>
        /// The log template should contain exactly one placeholder if no custom logger arguments are provided, which the elapsed
        /// time will replace. If custom arguments are provided, there should be exactly one more placeholder in the log template
        /// than the number of custom arguments, and the placeholder for the elapsed time should appear last.
        /// </summary>
        /// <param name="setup">The <c>Setup</c> instance.</param>
        /// <param name="logger">The logger to log with.</param>
        /// <param name="logLevel">The level to log at.</param>
        /// <param name="template">The log template.</param>
        /// <param name="args">Additional logger arguments.</param>
        /// <returns>The <c>Setup</c> instance via a chainable interface.</returns>
        /// <example>
        /// <code>
        /// setup.Log(logger, LogLevel.Debug, "Connected to {Server} in {Elapsed}", serverUri);
        /// </code>
        /// </example>
        public static IChainableDisposable <Setup> Log(
            this Setup setup,
            ILogger logger,
            LogLevel logLevel,
            string template,
            params object[] args)
        {
            if (logLevel == LogLevel.None)
            {
                return(setup);
            }

            Action <string, object[]> Log()
            {
                switch (logLevel)
                {
                case LogLevel.Trace: return(logger.LogTrace);

                case LogLevel.Debug: return(logger.LogDebug);

                case LogLevel.Information: return(logger.LogInformation);

                case LogLevel.Warning: return(logger.LogWarning);

                case LogLevel.Error: return(logger.LogError);

                case LogLevel.Critical: return(logger.LogCritical);

                default: throw new ArgumentOutOfRangeException();
                }
            }

            return(setup.Do(elapsed => Log()(template, args.Append(elapsed).ToArray())));
        }
Beispiel #2
0
        /// <summary>
        /// Configures an exception to be thrown if the code region takes longer to execute than a specified timeout.
        /// </summary>
        /// <param name="setup">The <c>Setup</c> instance.</param>
        /// <param name="timeout">The maximum permitted time for the code region to execute.</param>
        /// <returns>The <c>Setup</c> instance via a chainable interface.</returns>
        /// <exception cref="TimeoutException">
        /// The code region takes longer to execute than <paramref name="timeout" />
        /// </exception>
        public static IChainableDisposable <Setup> ThrowIfLongerThan(this Setup setup, TimeSpan timeout)
        {
            void Callback(TimeSpan elapsed)
            {
                if (elapsed > timeout)
                {
                    throw new TimeoutException($"Code executed in {elapsed} (exceeds timeout: {timeout}");
                }
            }

            return(setup.Do(Callback));
        }
Beispiel #3
0
 /// <summary>
 /// Configures the elapsed execution time to be dumped to SharpPad.
 /// </summary>
 /// <param name="setup">The <c>Setup</c> instance.</param>
 /// <param name="title">The title to associate with the elapsed time in the SharpPad pane.</param>
 /// <returns>The <c>Setup</c> instance via a chainable interface.</returns>
 /// <remarks>
 /// The dump to SharpPad will be executed asynchronously. There is no guarantee that this dump will have
 /// completed before further configured actions on <paramref name="setup"/> begin executing.
 /// </remarks>
 /// <example>
 /// <code>
 /// setup.DumpToSharpPad("Elapsed time for algorithm");
 /// </code>
 /// </example>
 public static IChainableDisposable <Setup> DumpToSharpPad(this Setup setup, string title) =>
 setup.Do(elapsed => elapsed.Dump(title));
Beispiel #4
0
 /// <summary>
 /// Configures the elapsed execution time to be dumped to SharpPad.
 /// </summary>
 /// <param name="setup">The <c>Setup</c> instance.</param>
 /// <param name="title">The title to associate with the elapsed time in the SharpPad pane.</param>
 /// <param name="port">The port the SharpPad server is running on.</param>
 /// <returns>The <c>Setup</c> instance via a chainable interface.</returns>
 /// <remarks>
 /// The dump to SharpPad will be executed asynchronously. There is no guarantee that this dump will have
 /// completed before further configured actions on <paramref name="setup"/> begin executing.
 /// </remarks>
 /// <example>
 /// <code>
 /// setup.DumpToSharpPad("Elapsed time for algorithm", 45960);
 /// </code>
 /// </example>
 public static IChainableDisposable <Setup> DumpToSharpPad(this Setup setup, string title, int port) =>
 setup.Do(elapsed => elapsed.DumpOnPort(port, title));
Beispiel #5
0
 /// <summary>
 /// Configures the elapsed execution time to be dumped to SharpPad.
 /// </summary>
 /// <param name="setup">The <c>Setup</c> instance.</param>
 /// <returns>The <c>Setup</c> instance via a chainable interface.</returns>
 /// <remarks>
 /// The dump to SharpPad will be executed asynchronously. There is no guarantee that this dump will have
 /// completed before further configured actions on <paramref name="setup"/> begin executing.
 /// </remarks>
 /// <example>
 /// <code>
 /// setup.DumpToSharpPad();
 /// </code>
 /// </example>
 public static IChainableDisposable <Setup> DumpToSharpPad(this Setup setup) =>
 setup.Do(elapsed => elapsed.Dump());