Sprint.Shared.Notification
1.1.0
See the version list below for details.
dotnet add package Sprint.Shared.Notification --version 1.1.0
NuGet\Install-Package Sprint.Shared.Notification -Version 1.1.0
<PackageReference Include="Sprint.Shared.Notification" Version="1.1.0" />
<PackageVersion Include="Sprint.Shared.Notification" Version="1.1.0" />
<PackageReference Include="Sprint.Shared.Notification" />
paket add Sprint.Shared.Notification --version 1.1.0
#r "nuget: Sprint.Shared.Notification, 1.1.0"
#:package Sprint.Shared.Notification@1.1.0
#addin nuget:?package=Sprint.Shared.Notification&version=1.1.0
#tool nuget:?package=Sprint.Shared.Notification&version=1.1.0
🚀 Sprint Shared Notification
A highly extensible and centralized Microsoft Teams Notification Service for .NET applications. It supports Multi-Targeting (.NET 6, 8, 10) and provides seamless integration with Power Automate Adaptive Cards.
✨ Features
- Multi-Target Support: Compatible with .NET 6, 8 (LTS), and 10.
- Adaptive Cards Integration: Richly formatted message payloads for better visibility in Teams.
- Global Error Handling: Automated
TeamsAlertMiddlewareto catch and report unhandled exceptions. - Flexible Customization: Powerful
CustomMessageFormatterto inject request context and external service data. - Clean API: Minimal setup with extension methods for both
IServiceCollectionandIApplicationBuilder.
🛠️ Configuration & Setup
There are three ways to register the notification service, depending on your preferred style.
Option 1: Using IConfiguration (Recommended)
This approach automatically binds values from the Notifications section in your appsettings.json.
appsettings.json Template:
{
"Notifications": {
"TeamsWebhookUrl": "https://m365x.webhook.office.com/...",
"ApplicationName": "SPRINT-ORDER-API",
"EnableNotification": true
}
}
Program.cs Setup:
// Simplest way: Reads from IConfiguration automatically
builder.Services.AddTeamsNotification(builder.Configuration);
// Or with additional overrides:
builder.Services.AddTeamsNotification(builder.Configuration, options =>
{
options.ApplicationName = "OVERRIDDEN-NAME"; // Further customization if needed
});
Option 2: Using Action Delegate
Fully manual setup without relying on appsettings.json sections.
builder.Services.AddTeamsNotification(options =>
{
options.TeamsWebhookUrl = "https://your-webhook-url";
options.ApplicationName = "MANUAL-SETUP-API";
options.EnableNotification = true;
});
Option 3: Using Pre-configured Object
Useful when you already have an options object or values from other sources.
var teamsOptions = new TeamsNotificationOptions
{
TeamsWebhookUrl = "https://your-webhook-url",
ApplicationName = "OBJECT-BASED-SETUP",
EnableNotification = true
};
builder.Services.AddTeamsNotification(teamsOptions);
4. Configure Middleware
Place UseTeamsAlert as early as possible (before standard error handling) to capture all unhandled exceptions.
app.UseTeamsAlert();
🎨 Creative Custom Message Formatting
The CustomMessageFormatter allows you to customize the message sent to Teams when an exception occurs.
Example 1: Extracting Detailed Request Info
You can extract headers, query strings, and body information from the HttpContext.
options.CustomMessageFormatter = (ctx, ex) =>
{
var method = ctx.Request.Method;
var path = ctx.Request.Path;
var userAgent = ctx.Request.Headers["User-Agent"];
var traceId = ctx.TraceIdentifier;
return $"**Method:** {method}\n" +
$"**Path:** {path}\n" +
$"**Agent:** {userAgent}\n" +
$"**Trace ID:** `{traceId}`";
};
Example 2: Accessing External Services via DI
Use ctx.RequestServices to access your registered services (e.g., IUserContext, ICurrentUser) to identify the user who encountered the error.
options.CustomMessageFormatter = (ctx, ex) =>
{
// Access registered services from the DI container
var userService = ctx.RequestServices.GetRequiredService<ICurrentUserService>();
var userName = userService.UserName ?? "Anonymous";
var userIp = ctx.Connection.RemoteIpAddress?.ToString() ?? "Unknown";
return $"**User:** {userName}\n" +
$"**IP Address:** {userIp}";
};
Example 3: Full Context (Request + User + Response)
options.CustomMessageFormatter = (ctx, ex) =>
{
var sb = new StringBuilder();
sb.AppendLine($"🆘 **Endpoint:** {ctx.Request.Method} {ctx.Request.Path}");
// Auth Information
if (ctx.User.Identity?.IsAuthenticated == true)
{
sb.AppendLine($"👤 **User:** {ctx.User.Identity.Name}");
}
// Query String
if (ctx.Request.QueryString.HasValue)
{
sb.AppendLine($"❓ **Query:** {ctx.Request.QueryString.Value}");
}
// Trace ID for correlation with logs
sb.AppendLine($"🆔 **Trace ID:** `{ctx.TraceIdentifier}`");
return sb.ToString();
};
📤 Manual Notification Usage
Inject INotificationService into your handlers or controllers.
public class OrderService
{
private readonly INotificationService _notifier;
public OrderService(INotificationService notifier) => _notifier = notifier;
public async Task ProcessOrderAsync(Order order)
{
try
{
// Do something...
await _notifier.SendMessageAsync("📦 Order Status", $"Order #{order.Id} has been processed.");
}
catch (Exception ex)
{
await _notifier.SendErrorAsync(ex, $"Failed to process Order #{order.Id}");
throw;
}
}
}
📄 License
Released under the MIT License.
| Product | Versions 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 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 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. |
-
net10.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Http (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.5)
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Sprint.Shared.Notification:
| Package | Downloads |
|---|---|
|
Sprint.Shared.Infrastructure
Shared infrastructure library for Sprint OMS services (.NET 10+): base handlers/entities, repositories and unit of work, FastEndpoints/OpenAPI helpers, caching (Redis + output cache), gRPC interceptors, observability (OpenTelemetry), Hangfire scheduling, adaptive concurrency, column encryption and HTTP client helpers. |
GitHub repositories
This package is not used by any popular GitHub repositories.