< Summary

Information
Line coverage
80%
Covered lines: 4
Uncovered lines: 1
Coverable lines: 5
Total lines: 71
Line coverage: 80%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MeasureTime(...)50%22100%
MeasureTime(...)100%11100%
MeasureTime(...)100%11100%
MeasureTime(...)100%11100%
MeasureTime(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Logging/Extensions/LoggerExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="LoggerExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Runtime.CompilerServices;
 9using Microsoft.Extensions.Logging;
 10using MyNet.Utilities.Logging;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Utilities;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16public static class LoggerExtensions
 17{
 18    extension(ILogger logger)
 19    {
 20        /// <summary>
 21        /// Measures the execution time of a code block and logs it at the specified log level. The log entry will inclu
 22        /// </summary>
 23        /// <param name="level">The log level at which to log the execution time.</param>
 24        /// <param name="title">The title to include in the log entry. If null, the caller member name will be used.</pa
 25        /// <param name="memberName">The name of the calling member. This is automatically provided by the compiler.</pa
 26        /// <returns>An IDisposable that, when disposed, logs the elapsed time.</returns>
 27        public IDisposable MeasureTime(LogLevel level = LogLevel.Trace, string? title = null, [CallerMemberName] string 
 328            => new PerformanceLogger(logger, title ?? memberName, level);
 29
 30        /// <summary>
 31        /// Measures the execution time of a code block and logs it. The log entry will include the provided title.
 32        /// </summary>
 33        /// <param name="title">The title to include in the log entry.</param>
 34        /// <param name="level">The log level at which to log the execution time.</param>
 35        /// <returns>An IDisposable that, when disposed, logs the elapsed time.</returns>
 36        public IDisposable MeasureTime(string title, LogLevel level = LogLevel.Trace)
 1237            => new PerformanceLogger(logger, title, level);
 38
 39        /// <summary>
 40        /// Measures the execution time of a code block and logs it with a dynamic log level based on elapsed time.
 41        /// </summary>
 42        /// <param name="provideLogLevel">A function that determines the log level based on the elapsed time.</param>
 43        /// <param name="title">The title to include in the log entry.</param>
 44        /// <returns>An IDisposable that, when disposed, logs the elapsed time.</returns>
 45        public IDisposable MeasureTime(Func<TimeSpan, LogLevel> provideLogLevel, string title)
 346            => new PerformanceLogger(logger, title, provideLogLevel);
 47
 48        /// <summary>
 49        /// Measures the execution time of a code block and logs it with custom settings.
 50        /// </summary>
 51        /// <param name="title">The title to include in the log entry.</param>
 52        /// <param name="showStartMessage">Whether to show a start message when the performance logger is created.</para
 53        /// <param name="showEndMessage">Whether to show an end message when the performance logger is disposed.</param>
 54        /// <param name="level">The log level at which to log the execution time.</param>
 55        /// <returns>An IDisposable that, when disposed, logs the elapsed time.</returns>
 56        public IDisposable MeasureTime(string title, bool showStartMessage, bool showEndMessage, LogLevel level = LogLev
 957            => new PerformanceLogger(logger, title, level, showStartMessage, showEndMessage);
 58
 59        /// <summary>
 60        /// Measures the execution time of a code block and logs it with a dynamic log level based on elapsed time and c
 61        /// </summary>
 62        /// <param name="title">The title to include in the log entry.</param>
 63        /// <param name="showStartMessage">Whether to show a start message when the performance logger is created.</para
 64        /// <param name="showEndMessage">Whether to show an end message when the performance logger is disposed.</param>
 65        /// <param name="provideLogLevel">A function that determines the log level based on the elapsed time.</param>
 66        /// <returns>An IDisposable that, when disposed, logs the elapsed time.</returns>
 67        public IDisposable MeasureTime(string title, bool showStartMessage, bool showEndMessage, Func<TimeSpan, LogLevel
 068            => new PerformanceLogger(logger, title, provideLogLevel, showStartMessage, showEndMessage);
 69    }
 70}
 71