| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LoggerExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using MyNet.Utilities.Logging; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Utilities; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public 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 |
| | 3 | 28 | | => 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) |
| | 12 | 37 | | => 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) |
| | 3 | 46 | | => 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 |
| | 9 | 57 | | => 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 |
| | 0 | 68 | | => new PerformanceLogger(logger, title, provideLogLevel, showStartMessage, showEndMessage); |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |