Treblle with .NET
Requirements
Section titled “Requirements”- .NET Framework 4.5 or higher
- Newtonsoft.Json 13.0.3+
Getting started
Section titled “Getting started”Create a FREE account on treblle.com to get an API key and Project ID.
You can install Treblle .NET via NuGet Package Manager or by running the following command:
Install-Package Treblle.NetYou will be prompted to enter your Treblle API key and Project ID. Your settings will be saved in Web.config and you can always edit them there.
Here is an example:
<configuration> <appSettings> <add key="TreblleApiKey" value="{Your_API_Key}" /> <add key="TreblleProjectId" value="{Your_Project_Id}" /> </appSettings></configuration>Now you can specify which endpoints you want Treblle to track by adding this simple attribute to any API controller or method:
[Treblle]That’s it. Your API requests and responses are now being sent to your Treblle project. Just by adding a few lines of code you get features like: auto-documentation, real-time request/response monitoring, error tracking and so much more.
Supported Integration Types
Section titled “Supported Integration Types”Treblle .NET supports both traditional ASP.NET Web API and Windows Communication Foundation (WCF) services.
ASP.NET Web API
Section titled “ASP.NET Web API”For ASP.NET Web API, use the [Treblle] attribute on controllers or methods:
[Treblle]public class ProductsController : ApiController{ public IHttpActionResult Get() { return Ok(/* your data */); }}Windows Communication Foundation (WCF)
Section titled “Windows Communication Foundation (WCF)”For WCF services, use the [TreblleBehavior] attribute on service classes or operation methods:
[TreblleBehavior]public class UserService : IUserService{ public UserResponse GetUser(int id) { // Operation is tracked return /* your data */; }}ASP.NET Web API Integration
Section titled “ASP.NET Web API Integration”Tracking Entire Controller
Section titled “Tracking Entire Controller”using Treblle.Net;
[Treblle]public class OrdersController : ApiController{ public IHttpActionResult Get() { var orders = _orderService.GetAll(); return Ok(orders); }
public IHttpActionResult Get(int id) { var order = _orderService.GetById(id); return Ok(order); }
public IHttpActionResult Post(OrderRequest request) { var order = _orderService.Create(request); return Created($"orders/{order.Id}", order); }}Tracking Specific Methods
Section titled “Tracking Specific Methods”public class ProductsController : ApiController{ [Treblle] public IHttpActionResult GetPublic() { // This endpoint is tracked return Ok(_productService.GetAll()); }
public IHttpActionResult GetInternal() { // This endpoint is NOT tracked return Ok(_productService.GetInternalProducts()); }}WCF Integration
Section titled “WCF Integration”Requirements
Section titled “Requirements”- .NET Framework 4.5 or higher
- WCF / System.ServiceModel
- Newtonship.Json 13.0.3+
Tracking Entire Service
Section titled “Tracking Entire Service”Apply the [TreblleBehavior] attribute to your WCF service class to track all operations:
using Treblle.Net;
[TreblleBehavior]public class UserService : IUserService{ public UserResponse GetUser(int id) { // This operation is tracked return _repository.GetUser(id); }
public async Task<UserResponse> CreateUserAsync(UserRequest request) { // This async operation is tracked return await _repository.CreateAsync(request); }
public UserResponse UpdateUser(int id, UserRequest request) { // This operation is tracked return _repository.Update(id, request); }
public void DeleteUser(int id) { // This operation is tracked _repository.Delete(id); }}Tracking Specific Operations
Section titled “Tracking Specific Operations”Apply the [TreblleBehavior] attribute to individual service methods to track only specific operations:
public class OrderService : IOrderService{ [TreblleBehavior] public OrderResponse GetOrder(int orderId) { // This operation is tracked return _repository.GetOrder(orderId); }
[TreblleBehavior] public async Task<OrderResponse> CreateOrderAsync(OrderRequest request) { // This async operation is tracked return await _repository.CreateAsync(request); }
public OrderResponse GetInternalOrder(int orderId) { // This operation is NOT tracked return _repository.GetInternalOrder(orderId); }}WCF Service Configuration
Section titled “WCF Service Configuration”Ensure your WCF service is properly configured in Web.config:
<system.serviceModel> <services> <service name="YourNamespace.UserService" behaviorConfiguration="ServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="YourNamespace.IUserService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8000/UserService" /> </baseAddresses> </host> </service> </services>
<behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" /></system.serviceModel>Supported WCF Scenarios
Section titled “Supported WCF Scenarios”Synchronous Operations
Section titled “Synchronous Operations”[TreblleBehavior]public UserResponse GetUser(int id){ return _userService.GetById(id);}Asynchronous Operations
Section titled “Asynchronous Operations”[TreblleBehavior]public async Task<UserResponse> GetUserAsync(int id){ return await _userService.GetByIdAsync(id);}Operations with Complex Types
Section titled “Operations with Complex Types”[TreblleBehavior]public OrderResponse CreateOrder(OrderRequest request){ // Request and response complex types are fully tracked return _orderService.Create(request);}Operations with Multiple Parameters
Section titled “Operations with Multiple Parameters”[TreblleBehavior]public SearchResponse Search(string query, int pageNumber, int pageSize){ // All parameters are tracked return _searchService.Execute(query, pageNumber, pageSize);}Data Captured for WCF Operations
Section titled “Data Captured for WCF Operations”For each tracked WCF operation, Treblle captures:
- Request Information: Service method name, parameters, headers, and body
- Response Information: Return value, headers, and response status
- Performance Metrics: Execution time and response size
- Error Information: Exception details and fault strings
- Server Context: Host information, environment, and timing
- Security: Automatic masking of sensitive data
Configuration Options
Section titled “Configuration Options”Need to hide additional fields?
Section titled “Need to hide additional fields?”If you want to expand the list of fields you want to hide, you can pass property names you want to hide by adding the AdditionalFieldsToMask property to your Web.config file like in the example below.
<configuration> <appSettings> <add key="TreblleApiKey" value="{Your_API_Key}" /> <add key="TreblleProjectId" value="{Your_Project_Id}" /> <add key="AdditionalFieldsToMask" value="secretField,highlySensitiveField" /> </appSettings></configuration>Field Masking
Section titled “Field Masking”Treblle masks sensitive information from both the request and response data as well as the request headers data before it even leaves your server. The following parameters are automatically masked: password, pwd, secret, password_confirmation, cc, card_number, ccv, ssn, credit_score.
This masking applies to both Web API and WCF services.