Integrate AuditKit into your .NET application with the official C# SDK. Middleware, Entity Framework Core interceptors, and ASP.NET Core integration.
The AuditKit .NET SDK provides first-class integration with ASP.NET Core and Entity Framework Core. Use middleware for automatic request logging, EF Core interceptors for database change tracking, and the fluent API for explicit event capture. The SDK supports dependency injection, IHostedService for background delivery, and configuration through appsettings.json or environment variables.
Add AuditKit to your .NET project.
dotnet add package AuditKit.AspNetCoreRegister AuditKit in your DI container.
// Program.cs
builder.Services.AddAuditKit(options =>
{
options.ApiKey = builder.Configuration["AuditKit:ApiKey"];
options.TenantResolver = context =>
context.User.FindFirst("org_id")?.Value;
});Register the AuditKit middleware in the pipeline.
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseAuditKit(); // Add after auth middleware
app.MapControllers();Inject IAuditKitClient and log events in controllers or services.
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly IAuditKitClient _auditkit;
public UsersController(IAuditKitClient auditkit)
{
_auditkit = auditkit;
}
[HttpPost("{id}/role")]
public async Task<IActionResult> ChangeRole(string id, RoleUpdate model)
{
var user = await _userService.ChangeRole(id, model.Role);
await _auditkit.LogAsync(new AuditEvent
{
Action = "user.role_changed",
Actor = new Actor { Id = User.GetUserId(), Email = User.GetEmail() },
Target = new Target { Type = "user", Id = id },
Metadata = new { PreviousRole = user.PreviousRole, NewRole = model.Role },
});
return Ok(user);
}
}Here is a complete example showing AuditKit integrated into a .NET application with authentication logging, data access tracking, and explicit event capture.
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuditKit(options =>
{
options.ApiKey = builder.Configuration["AuditKit:ApiKey"];
options.TenantResolver = context =>
context.User.FindFirst("org_id")?.Value;
options.EnableEfCoreInterceptor = true;
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseAuditKit();
app.MapControllers();
app.Run();
// Controllers/InvoiceController.cs
[ApiController]
[Route("api/[controller]")]
public class InvoicesController : ControllerBase
{
private readonly IAuditKitClient _auditkit;
private readonly IInvoiceService _invoiceService;
public InvoicesController(IAuditKitClient auditkit, IInvoiceService service)
{
_auditkit = auditkit;
_invoiceService = service;
}
[HttpPost("{id}/refund")]
public async Task<IActionResult> Refund(string id)
{
var invoice = await _invoiceService.GetByIdAsync(id);
var previousStatus = invoice.Status;
await _invoiceService.RefundAsync(id);
await _auditkit.LogAsync(new AuditEvent
{
Action = "invoice.refunded",
Actor = new Actor
{
Id = User.GetUserId(),
Email = User.GetEmail(),
},
Target = new Target { Type = "invoice", Id = id },
Metadata = new
{
InvoiceNumber = invoice.Number,
Amount = invoice.Amount,
PreviousStatus = previousStatus,
},
});
return Ok(invoice);
}
}ASP.NET Core middleware for automatic request logging
EF Core interceptors for database change tracking
ASP.NET Core Identity event handlers for auth logging
IHostedService for reliable background delivery
Minimal API integration with endpoint filters
MediatR pipeline behavior for CQRS command logging
Install AuditKit.AspNetCore from NuGet, call AddAuditKit() in your service configuration, and add UseAuditKit() to your middleware pipeline. Inject IAuditKitClient into controllers or services for explicit event logging.
Yes. The AuditKit .NET SDK includes an EF Core interceptor that automatically captures database mutations (insert, update, delete) as audit events with before/after state tracking. Enable it with EnableEfCoreInterceptor = true in your configuration.
Get started with tamper-proof audit trails in minutes. Open source, from $99/mo.