This sample project demonstrates how to integrate Split.io feature flags into an ASP.NET Core web application using a production-ready approach with proper dependency injection and logging.
- Split.io SDK integration with ASP.NET Core
- Custom logging implementation using Serilog
- Dependency injection of the Split.io client
- Example endpoint demonstrating feature flag usage
- Structured logging configuration
Program.cs- Main application entry point with Split.io configurationCustomLoggerImplementation.cs- Custom logger implementation bridging Split.io logs to Serilogappsettings.json- Configuration file with Split.io API key and Serilog settings
- .NET 9.0 SDK or later
- Split.io account and API key
- Clone the repository
- Replace the placeholder value in
appsettings.jsonwith your actual Split.io API key:
"SplitIO": {
"ApiKey": "YOUR_SPLIT_IO_API_KEY"
}- Run the application:
dotnet runThe application exposes an example endpoint that demonstrates how to use the Split.io client to get treatments for feature flags:
GET /split-example/{userId}/{featureFlag}
Example:
curl http://localhost:5238/split-example/testuser/test-feature
Response:
User testuser receives treatment 'control' for feature 'test-feature'
The Split.io client is configured in Program.cs using a configuration value from appsettings.json:
var config = new ConfigurationOptions();
config.Logger = new CustomLoggerImplementation();
var apiKey = builder.Configuration["SplitIO:ApiKey"] ?? throw new InvalidOperationException("SplitIO API key not found in configuration");
var factory = new SplitFactory(apiKey, config);
var splitClient = factory.Client();A custom implementation of the ISplitLogger interface bridges Split.io logs to Serilog:
public class CustomLoggerImplementation : ISplitLogger
{
public bool IsDebugEnabled => true;
public void Debug(string message)
{
Log.Debug(message);
}
// Additional methods...
}The Split.io client is registered as a singleton service and available throughout the application:
builder.Services.AddSingleton<ISplitClient>(splitClient);The Split.io client is injected into endpoints and used to get treatments:
app.MapGet("/split-example/{userId}/{featureFlag}", (string userId, string featureFlag, ISplitClient client) =>
{
var treatment = client.GetTreatment(userId, featureFlag);
return $"User {userId} receives treatment '{treatment}' for feature '{featureFlag}'";
});