Treblle with .NET Core
Requirements
- .NET: 6.0+
- ASP.NET Core: 6.0+
- C#: 10.0+
Supported Platforms:
- Windows, macOS, Linux
- Docker containers
- Azure, AWS, Google Cloud
- Any hosting platform supporting .NET 8+
Supported API Types:
- REST APIs with Controllers
- Minimal APIs
- Web APIs
- Mixed controller/minimal API applications
Installation
You can install Treblle .NET Core via NuGet . Simply run the following command:
dotnet add package Treblle.Net.CoreGet Your Credentials
Get your SDK Token and API Key from the Treblle Dashboard .
Configuration
Treblle v2.0 now supports automatic endpoint discovery, so you no more need to manually add `[Treblle]` attributes.
Choose from these configuration options:
Option A: Environment Variables (Recommended for Production)
export TREBLLE_SDK_TOKEN=your_sdk_token
export TREBLLE_API_KEY=your_api_keyThen use zero-configuration setup:
using Treblle.Net.Core;
// Register Treblle Services
builder.Services.AddTreblle();
// Build your application
var app = builder.Build();
// Enable the Treblle Middleware
app.UseTreblle();Note
Where to place this code:
- Program.cs (new minimal hosting model): Add `builder.Services.AddTreblle()` before `builder.Build()` and `app.UseTreblle()` after `var app = builder.Build()`
- Startup.cs (legacy): Add `services.AddTreblle()` in `ConfigureServices()` and `app.UseTreblle()` in `Configure()`
- Web API templates: Place after authentication/authorization middleware but before routing
Using .env Files with DotNetEnv
For development environments, you can use `.env` files:
dotnet add package DotNetEnvCreate a `.env` file in your project root:
TREBLLE_SDK_TOKEN=your_sdk_token
TREBLLE_API_KEY=your_api_keyThen load the environment variables:
using Treblle.Net.Core;
using DotNetEnv;
// Load environment variables from .env file
Env.Load();
// Register Treblle Services
builder.Services.AddTreblle();
var app = builder.Build();
app.UseTreblle();Option B: appsettings.json
{
"Treblle": {
"SdkToken": "your_sdk_token",
"ApiKey": "your_api_key"
}
}using Treblle.Net.Core;
builder.Services.AddTreblle();
var app = builder.Build();
app.UseTreblle();Option C: Manual Configuration
using Treblle.Net.Core;
builder.Services.AddTreblle("your_sdk_token", "your_api_key");
var app = builder.Build();
app.UseTreblle();That’s it! Treblle will now automatically track all your API endpoints.
Advanced Configuration
Option
Default and Description
ExcludedPaths
null - Exclude specific paths or endpoints from Treblle
DebugMode
false - Enable detailed logging for troubleshooting
DisableMasking
false - Disable data masking if not needed
Example with all options:
using Treblle.Net.Core;
builder.Services.AddTreblle(options =>
{
options.ExcludedPaths = new[] { "/health", "/admin/*", "/swagger/*" };
options.DebugMode = true;
options.DisableMasking = false;
});
var app = builder.Build();
app.UseTreblle();Exception Handling Configuration
The Treblle .NET Core SDK includes built-in exception handling capabilities. As of v2.0.4, the useExceptionHandler parameter is enabled by default and helps capture API errors without exposing sensitive information in response bodies.
Usage
// Import the Treblle SDK
using Treblle.Net.Core;
// Register Treblle Services
builder.Services.AddTreblle();
// Build your application
var app = builder.Build();
// Enable the Treblle Middleware with exception handler (enabled by default in v2.0.4+)
app.UseTreblle();
// Or explicitly disable if needed
app.UseTreblle(useExceptionHandler: false);Configuration Options
The useExceptionHandler parameter controls whether Treblle should use its built-in exception handling middleware:
true(default in v2.0.4+): Enables automatic exception capture and error trackingfalse: Disables the exception handler if you’re using a custom error handling solution
Example with Exception Handling
// Import the Treblle SDK
using Treblle.Net.Core;
var builder = WebApplication.CreateBuilder(args);
// Register Treblle Services
builder.Services.AddTreblle();
// Build your application
var app = builder.Build();
// Enable the Treblle Middleware (useExceptionHandler is true by default)
app.UseTreblle();
app.MapControllers();
app.Run();What It Does
When useExceptionHandler is enabled:
- Captures exceptions thrown by your API endpoints
- Logs error details to Treblle for monitoring and debugging
- Prevents sensitive information from being exposed in error responses
- Records 500-level errors with proper error tracking
Note
In v2.0.4 and later, the duplicate request logging issue that occurred when exceptions were thrown has been resolved. The exception handler now correctly tracks errors without creating duplicate entries in your Treblle dashboard.
Tip
If you have problems of any kind, feel free to reach out via email support@treblle.com and we’ll do our best to help you out.