Skip to content

Treblle with .NET

  • .NET Framework 4.5 or higher
  • Newtonsoft.Json 13.0.3+

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:

Terminal window
Install-Package Treblle.Net

You 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.

Treblle .NET supports both traditional ASP.NET Web API and Windows Communication Foundation (WCF) services.

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 */);
}
}

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 */;
}
}

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);
}
}
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());
}
}

  • .NET Framework 4.5 or higher
  • WCF / System.ServiceModel
  • Newtonship.Json 13.0.3+

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);
}
}

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);
}
}

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>
[TreblleBehavior]
public UserResponse GetUser(int id)
{
return _userService.GetById(id);
}
[TreblleBehavior]
public async Task<UserResponse> GetUserAsync(int id)
{
return await _userService.GetByIdAsync(id);
}
[TreblleBehavior]
public OrderResponse CreateOrder(OrderRequest request)
{
// Request and response complex types are fully tracked
return _orderService.Create(request);
}
[TreblleBehavior]
public SearchResponse Search(string query, int pageNumber, int pageSize)
{
// All parameters are tracked
return _searchService.Execute(query, pageNumber, pageSize);
}

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

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>

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.