SnippetDurationLogger 1.0.0

dotnet add package SnippetDurationLogger --version 1.0.0                
NuGet\Install-Package SnippetDurationLogger -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SnippetDurationLogger" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SnippetDurationLogger --version 1.0.0                
#r "nuget: SnippetDurationLogger, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SnippetDurationLogger as a Cake Addin
#addin nuget:?package=SnippetDurationLogger&version=1.0.0

// Install SnippetDurationLogger as a Cake Tool
#tool nuget:?package=SnippetDurationLogger&version=1.0.0                

Code Snippet Duration Logger

This NuGet Package has been developed for scenarios where a developer wishes to capture the execution time of a snippet of code. Not the execution time for a full method but a snippet of code in it, and then log it.

This package will extend your Logger with a static method called LogExecutionTime.

Your Class

  • Add a reference to the SnippetDuration.Classes namespace
using SnippetDuration.Classes;
  • When calling the LogExecutionTime extension method you can pass in a simple comment, that will form part of the log too. This can help you find the logging details within your log file.

Add the logger extension method LogExecutionTime around the code you wish to capture the execution time for

    _logger.LogExecutionTime("Add simple comment...", () =>
    {
        // Your existing code block, whose execution time will be logged        
    });

Example Code Usage

Below is a full class examaple of using the Code Snippet Duration Timer

using Microsoft.Extensions.Logging;
using SnippetDuration.Classes;

namespace SnippetDurationLoggerConsole
{
    internal class ExternalMethods
    {
        private readonly ILogger<ExternalMethods> _logger;
        public ExternalMethods(ILogger<ExternalMethods> logger)
        {
            _logger = logger; // injected logger
        }

        public void LongRunningprocess()
        {
           _logger.LogExecutionTime("Testing DB Call", () =>              // <-- Add this block of code
            {
                // Code block whose execution time will be logged
                System.Threading.Thread.Sleep(3500); // Simulate a long running process
            });
        }
    }
}

Example Log File Output

[2024-10-06 19:46:05 INF] Testing DB Call - Method :: LongRunningprocess - took 3518 ms

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 93 10/6/2024