RootAlert.Redis 0.1.0

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

🚀 RootAlert - Real-time Exception Tracking for .NET

NuGet Badge

RootAlert is a lightweight real-time error tracking and alerting library for .NET applications. It captures unhandled exceptions, batches them intelligently, and sends alerts to Microsoft Teams and Slack.

🔥 Features

  • 🛠 Automatic exception handling via middleware
  • 🚀 Real-time alerts with batching to prevent spam
  • 📡 Supports Microsoft Teams (Adaptive Cards) & Slack (Blocks & Sections)
  • Customizable batch interval using TimeSpan
  • 📩 Rich error logs including request details, headers, and stack traces
  • 🔗 Supports Redis and MSSQL for persistent storage

📦 Installation

RootAlert is available on NuGet. Install it using:

 dotnet add package RootAlert 

Or via Package Manager:

 Install-Package RootAlert 

⚡ Quick Start

1️⃣ Configure RootAlert in Program.cs

Add RootAlert to your services and configure it to send alerts to Microsoft Teams or Slack.

If you do not configure a storage option, RootAlert will default to in-memory storage.

using RootAlert.Config;
using RootAlert.Extensions;
using RootAlert.Storage;

var builder = WebApplication.CreateBuilder(args);

var rootAlertOptions = new List<RootAlertOption>
{
    new RootAlertOption
    {
        AlertMethod = AlertType.Teams,
        WebhookUrl = "https://your-teams-webhook-url"
    },
    new RootAlertOption
    {
        AlertMethod = AlertType.Slack,
        WebhookUrl = "https://your-slack-webhook-url"
    }
};

var rootAlertSetting = new RootAlertSetting
{
    BatchInterval = TimeSpan.FromSeconds(20),
    RootAlertOptions = rootAlertOptions,
};

builder.Services.AddRootAlert(rootAlertSetting);

var app = builder.Build();

// ✅ Handle exceptions first
app.UseMiddleware<ExceptionHandlingMiddleware>();

// ✅ Then, log errors with RootAlert
app.UseRootAlert();

app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

app.Run();

Now, RootAlert will automatically capture all unhandled exceptions!


⚡ Persistent Storage Options (Redis & MSSQL)

RootAlert now supports separate libraries for persistent storage:

  • 🔹 RootAlert.Redis → Stores logs in Redis
  • 🔹 RootAlert.MSSQL → Stores logs in SQL Server

🛠 RootAlert.Redis

Installation
 dotnet add package RootAlert.Redis 
Implementation
using RootAlert.Redis;

var rootAlertSetting = new RootAlertSetting
{
    Storage = new RedisAlertStorage("127.0.0.1:6379"),
    BatchInterval = TimeSpan.FromSeconds(20),
    RootAlertOptions = rootAlertOptions,
};

🔹 Why Use Redis Storage?

  • ✅ Ensures logs persist even if the app restarts or the App Pool recycles.
  • ✅ Allows centralized error tracking across multiple instances.
  • ✅ Ideal for distributed applications that run across multiple servers.

🛠 RootAlert.MSSQL

Installation
 dotnet add package RootAlert.MSSQL 
Implementation
using RootAlert.MSSQL;

var rootAlertSetting = new RootAlertSetting
{
    Storage = new MSSQLAlertStorage("Server=myServerAddress;Database=myDB;User Id=myUser;Password=myPassword;"),
    BatchInterval = TimeSpan.FromSeconds(20),
    RootAlertOptions = rootAlertOptions,
};
SQL Table Schema for MSSQL

To store logs in MSSQL, create the following table:

CREATE TABLE RootAlertLogs (
    Id INT IDENTITY PRIMARY KEY,
    ExceptionMessage NVARCHAR(MAX) NOT NULL,
    StackTrace NVARCHAR(MAX) NULL,
    ExceptionName NVARCHAR(255) NULL, 
    RequestUrl NVARCHAR(MAX) NULL, 
    HttpMethod NVARCHAR(10) NULL, 
    Headers NVARCHAR(MAX) NULL, 
    ErrorCount INT DEFAULT 1, 
    CreatedAt DATETIME2 DEFAULT GETUTCDATE(),
    Processed BIT DEFAULT 0
);

🔹 Why Use MSSQL Storage?

  • ✅ Stores logs in a structured database for easy querying.
  • ✅ Ideal for applications that require long-term error tracking.
  • ✅ Supports advanced analytics and monitoring tools.

⚠️ Important Notes

❗ If an exception filter is added, RootAlert won't work.

Reason: Exception filters handle errors before middleware gets a chance to process them. Since RootAlert works as middleware, it will never see the exception if a filter catches it first.

✅ Solution: Ensure RootAlert is added after any existing exception-handling middleware.

If your application has a global exception-handling middleware, register RootAlert after it to ensure exceptions are logged correctly. Example:

app.UseMiddleware<ExceptionHandlingMiddleware>(); // Your existing middleware
app.UseRootAlert(); // Register RootAlert after the exception middleware

🏆 Microsoft Teams Integration

RootAlert supports Microsoft Teams integration via:
1. Incoming Webhooks (Connector) – Simple and quick setup. (Will be deprecated)
2. Microsoft Teams Workflow API – Easier than Power Automate, with a built-in Webhook template.

🔹 Option 1: Using an Incoming Webhook (Connector)

This method is the easiest way to receive error alerts in a Teams channel.

📌 Steps to Get a Teams Webhook URL

1. Open **Microsoft Teams** and go to the desired channel.  
2. Click **"…" (More options) → Connectors**.  
3. Find **"Incoming Webhook"** and click **"Configure"**.  
4. Name it **RootAlert Notifications** and click **Create**.  
5. Copy the **Webhook URL** and use it in `RootAlertOptions`.
5. Copy the **Webhook URL** and use it in your RootAlert configuration.  

🔹 Option 2: Using Microsoft Teams Workflow API (via Webhook Template)

This method is even easier than Power Automate and uses a built-in workflow to receive data via Webhook. 🎥 Watch this video for a step-by-step guide:
Microsoft Teams Workflow API Setup
🔗 YouTube Link: https://www.youtube.com/watch?v=jHTU_jUnswY

📌 Steps to Configure Teams Workflow API

  1. Open Microsoft Teams and Go to Workflows
    • Click on “…” (More options) → Workflows. --> Create
  2. Select "Post to a channel when a webhook request is received" Template
    • Search for "Post to a channel when a webhook request is received" and select the ready-made template.
    • Click Next to proceed.
  3. Choose Team and Channel
    • Select the Team where you want to post alerts.
    • Choose the Channel where notifications should appear.

💬 Slack Integration

RootAlert supports Slack using Blocks & Sections for structured messages.

🔹 How to Get a Slack Webhook URL

  1. Go to https://api.slack.com/apps and create a new Slack App.
  2. Enable Incoming Webhooks under Features.
  3. Click "Add New Webhook to Workspace" and select a channel.
  4. Copy the Webhook URL and use it in RootAlertOptions.

⚙️ Configuration Options

Option Description
AlertMethod Teams or Slack (Choose alerting platform)
WebhookUrl Webhook URL for Teams or Slack
BatchInterval TimeSpan (e.g., TimeSpan.FromSeconds(20))
Storage Supports Redis (RedisAlertStorage) or MSSQL (SqlServerAlertStorage)

🚨 Example Batched Error Summary Alert

🚨 Root Alert - Batched Error Summary

🔴 Error #1
Error Count: 3
📅 Timestamp: 03/22/2025 6:30:29 PM
🌐 Request URL: /getuser
📡 HTTP Method: GET
📩 Request Headers:
Accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Host: api.example.com
----------------------------------------------------
⚠️ Exception Details
❗ Type: HttpRequestException
💬 Message: Weather API failed to respond
----------------------------------------------------
🔍 Stack Trace
   at WeatherService.GetWeatherData() in WeatherService.cs:line 45
   at RootAlertMiddleware.Invoke(HttpContext context)
----------------------------------------------------

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 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 is compatible.  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. 
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
0.1.0 158 3/22/2025