ApplicationMetrics.MetricLoggers.MongoDb 1.0.0

dotnet add package ApplicationMetrics.MetricLoggers.MongoDb --version 1.0.0
                    
NuGet\Install-Package ApplicationMetrics.MetricLoggers.MongoDb -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="ApplicationMetrics.MetricLoggers.MongoDb" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApplicationMetrics.MetricLoggers.MongoDb" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ApplicationMetrics.MetricLoggers.MongoDb" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApplicationMetrics.MetricLoggers.MongoDb --version 1.0.0
                    
#r "nuget: ApplicationMetrics.MetricLoggers.MongoDb, 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.
#:package ApplicationMetrics.MetricLoggers.MongoDb@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApplicationMetrics.MetricLoggers.MongoDb&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ApplicationMetrics.MetricLoggers.MongoDb&version=1.0.0
                    
Install as a Cake Tool

ApplicationMetrics.MetricLoggers.MongoDb

An implementation of an ApplicationMetrics metric logger which writes metrics and instrumentation to a MongoDB database.

Setup

The code below demonstrates the setup and use case (with fake metrics logged) of the MongoDbMetricLogger class...

using (var bufferProcessor = new SizeLimitedBufferProcessor(5))
using (var metricLogger = new MongoDbMetricLogger
(
    "DefaultCategory",
    "mongodb://127.0.0.1:27017",
    "ApplicationMetrics",
    bufferProcessor,
    IntervalMetricBaseTimeUnit.Nanosecond,
    true
))
{
    metricLogger.Start();

    Guid beginId = metricLogger.Begin(new MessageSendTime());
    Thread.Sleep(20);
    metricLogger.Increment(new MessageSent());
    metricLogger.Add(new MessageSize(), 2661);
    metricLogger.End(beginId, new MessageSendTime());

    metricLogger.Stop();
}

The MongoDbMetricLogger class accepts the following constructor parameters...

Parameter Name Description
category The category to log all metrics under.
connectionString The string to use to connect to MongoDB.
databaseName The name of the database.
bufferProcessingStrategy An object implementing IBufferProcessingStrategy which decides when the buffers holding logged metric events should be flushed (and be written to MongoDB).
intervalMetricBaseTimeUnit The base time unit to use to log interval metrics.
intervalMetricChecking Specifies whether an exception should be thrown if the correct order of interval metric logging is not followed (e.g. End() method called before Begin()). Note that this parameter only has an effect when running in 'non-interleaved' mode.
Storage

4 collections are created in the specified MongoDB database, which store the metrics...

Collection Name
CountMetricInstances
AmountMetricInstances
StatusMetricInstances
IntervalMetricInstances

Note that timestamps in MongoDB have only millisecond precision by default, hence metric events timestamps are stored in 2 properties in each document, as per the below table...

Property Name Contents
EventTime The timestamp of the event as a UTC BSON Date (to the closest millisecond).
EventTimeTicks The timestamp of the event as the number of Ticks elapsed since January 1, 0001 at 00:00:00.000 UTC. In .NET this can be converted to a DateTime by passing to the relevent DateTime constructor overload.

An example of event timestamps queried from MongoDB is as follows...

{
	"_id" : ObjectId("6979616fd47069ae771e0141"),
	"Category" : "DefaultCategory",
	"EventTime" : ISODate("2026-01-28T01:07:59.112Z"),
	"EventTimeTicks" : Long("639051592791127525"),
	"AmountMetric" : "DiskBytesRead",
	"Amount" : Long("2048")
}

Storing metric event timestamps using both properties gives both readability, and complete precision/accuracy.

Non-interleaved Method Overloads

Methods which support 'non-interleaved' interval metric logging (i.e. overloads of End() and CancelBegin() methods which don't accept a Guid) will be deprecated in a future version of ApplicationMetrics. Hence it's recommended to only use the End() and CancelBegin() method overloads which accept a 'beginId' Guid parameter.

The documentation below was written for version 1.* of ApplicationMetrics. Minor implementation details may have changed in versions 2.0.0 and above, however the basic principles and use cases documented are still valid. Note also that this documentation demonstrates the older 'non-interleaved' method of logging interval metrics.

Full documentation for the project...<br /> http://www.alastairwyse.net/methodinvocationremoting/application-metrics.html

A detailed sample implementation...<br /> http://www.alastairwyse.net/methodinvocationremoting/sample-application-5.html

Release History
Version Changes
1.0.0 Initial release.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 113 1/28/2026

Initial release.