APM-globalMiddleware 1.5.6

dotnet add package APM-globalMiddleware --version 1.5.6
                    
NuGet\Install-Package APM-globalMiddleware -Version 1.5.6
                    
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="APM-globalMiddleware" Version="1.5.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="APM-globalMiddleware" Version="1.5.6" />
                    
Directory.Packages.props
<PackageReference Include="APM-globalMiddleware" />
                    
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 APM-globalMiddleware --version 1.5.6
                    
#r "nuget: APM-globalMiddleware, 1.5.6"
                    
#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 APM-globalMiddleware@1.5.6
                    
#: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=APM-globalMiddleware&version=1.5.6
                    
Install as a Cake Addin
#tool nuget:?package=APM-globalMiddleware&version=1.5.6
                    
Install as a Cake Tool

APM-globalMiddleware

APM-globalMiddleware is a .NET 10 and .NET 8 library that provides global middleware for Application Performance Monitoring (APM) in .NET applications. It integrates with Application Insights and Serilog for advanced telemetry and structured logging, and supports both Azure Functions and ASP.NET Core scenarios.


Features

  • Automatic telemetry collection using Application Insights
  • Structured logging with Serilog, including Application Insights sink support
  • Middleware support for ASP.NET Core and Azure Functions
  • OpenAPI integration

Getting Started

Prerequisites

  • .NET 8.0 SDK or .NET 10 SDK

Installation

Using .NET CLI

dotnet add package APM-globalMiddleware

Using Package Manager

Install-Package APM-globalMiddleware

ASP.NET Core Example

var configuration = builder.Configuration;
Log.Logger = new LoggerConfiguration()
    .APMLoggerEnricher(configuration, forwardsAllLogs: false) // if sets to true, all logs will be forwarded
    .CreateLogger();

builder.Services.AddGlobalExceptionHandling(builder.Configuration);

app.UseGlobalExceptionHandling();  // Place the nuget config after all the middleware, this wil make it so all other error handeling is ignored and all errors are handled by the APM-globalMiddleware, if you want to use the APM-globalMiddleware in conjunction with other error handling middleware, place the APM-globalMiddleware before the other middleware.

appsettings.json

"ApplicationInsights": {
    "ConnectionString": "<Placeholder>"  //this can be empty, APM wil handle this internally if empty.
  },
  "ErrorHandling": {
    "Enabled": true, // this is an optional field that can be used to enable or disable the APM-globalMiddleware, if not set, it will default to true.
    "ServiceName": "<Service-placeholder>",
    "ApiKey": "<Api-Placeholder>", // you get this from the APM backend deployment API-key
    "ErrorUrl": "<Error-Placeholder>", //    url/api/Errorreceive
    "LogsUrl": "<Logs-Placeholder>", //      url/api/ErrorReceive/logs
    "UseAzureApplicationInsights": true, // this currently does nothing, but is reserved for future use
    "CatchHttpErrors": false
  }

Azure Functions Example

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

string aiConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
Log.Logger = new LoggerConfiguration()
    .APMLLoggerEnricher(aiConnectionString)
    .CreateLogger();

try
{
    builder.Services.AddGlobalExceptionHandlingForFunctions(builder.Configuration);
    builder.UseMiddleware<FunctionExceptionHandler>();
    builder.Services.AddSerilog(Log.Logger);

    builder.Build().Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
    Log.CloseAndFlush();
}

local.settings.json

"Values": {
    "Enabled": true, // this is an optional field that can be used to enable or disable the APM-globalMiddleware, if not set, it will default to true.
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "<Placeholder>",
    "ErrorHandling:ServiceName": "<Service-placeholder>",
    "ErrorHandling:ApiKey": "<Api-Placeholder>", // you get this from the APM backend deployment API-key
    "ErrorHandling:ErrorUrl": "<Error-Placeholder>",  //    url/api/Errorreceive
    "ErrorHandling:LogsUrl": "<Logs-Placeholder>", //      url/api/ErrorReceive/logs
    "ErrorHandling:UseAzureApplicationInsights": "true", // this currently does nothing, but is reserved for future use
    "ErrorHandling:CatchHttpErrors": "false" // this does nothing for Azure functions
  }

Hearthbeat Endpoint

APM-globalMiddleware includes a built-in heartbeat endpoint that can be used to monitor the health of your application. The endpoint is available at /api/heartbeat and returns a simple JSON response indicating the status of the application.

ASP.NET Core Example

app.UseApmHeartbeat();

Or

app.UseApmHeartbeat("/health"); // this supports a custom endpoint 

Metrics Integration

THIS IS CURRENTLY NOT IMPLEMENTED IN DE APM SO I SUGGEST TURNING THIS OFF OTHERWISE IT IS JUST A WASTE OF RESOURCES.

APM-globalMiddleware provides automatic collection and forwarding of custom metrics. This feature integrates seamlessly with your existing telemetry and logging infrastructure, allowing you to monitor application performance with minimal setup.

Initialization

To enable metrics collection, add the following line to your Program.cs or during service registration:

builder.Services.AddApmMetrics(builder.Configuration);

Configuration

"ErrorHandling": {
    "MetricsUrl": "<Metrics-Placeholder>"
}
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.  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 is compatible.  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. 
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.