AuditLogLib.AspNetCore
2.1.0
dotnet add package AuditLogLib.AspNetCore --version 2.1.0
NuGet\Install-Package AuditLogLib.AspNetCore -Version 2.1.0
<PackageReference Include="AuditLogLib.AspNetCore" Version="2.1.0" />
<PackageVersion Include="AuditLogLib.AspNetCore" Version="2.1.0" />
<PackageReference Include="AuditLogLib.AspNetCore" />
paket add AuditLogLib.AspNetCore --version 2.1.0
#r "nuget: AuditLogLib.AspNetCore, 2.1.0"
#:package AuditLogLib.AspNetCore@2.1.0
#addin nuget:?package=AuditLogLib.AspNetCore&version=2.1.0
#tool nuget:?package=AuditLogLib.AspNetCore&version=2.1.0
AuditLogLib.AspNetCore
Server side for audit-log-lib, the browser audit log library. Gives you a SignalR hub and an HTTP endpoint that receive batched audit logs from the browser, one interface to store them, and a service that sends commands back to connected browsers.
Live demo: the browser side with a simulated backend.
- .NET 10, no NuGet dependencies (uses the ASP.NET Core shared framework).
- Same version as the npm package; both follow the contract.
Setup
using AuditLogLib.AspNetCore;
builder.Services.AddAuditLog(o =>
{
o.MaxBatchSize = 200; // default
o.MaxReceiveMessageSize = 1_048_576; // SignalR limit for this hub only (default 1 MB)
});
builder.Services.AddScoped<IAuditLogSink, MyAuditLogSink>(); // optional, default writes to ILogger
app.MapAuditLogHub("/hubs/audit").RequireAuthorization(); // for SignalRTransport
app.MapAuditLogEndpoint("/api/audit-logs").RequireAuthorization(); // for HttpTransport
Browser side:
import { AuditLog, SignalRTransport } from 'audit-log-lib';
const audit = new AuditLog({
transport: new SignalRTransport({ url: 'https://your-api.com/hubs/audit' }),
});
Storing entries
Implement IAuditLogSink. It is scoped, so you can inject your DbContext:
public sealed class MyAuditLogSink(AppDbContext db) : IAuditLogSink
{
public async Task WriteAsync(AuditLogBatch batch, CancellationToken cancelToken = default)
{
foreach (var entry in batch.Entries)
{
db.AuditLogs.Add(new AuditLogRow
{
EntryId = entry.EntryId,
UserId = batch.UserId, // from the authenticated user, not from the browser
Action = entry.Action,
Level = entry.Level,
Payload = entry.Payload?.GetRawText(),
CreatedAt = entry.CreatedAt, // client clock
ReceivedAt = batch.ReceivedAt, // server clock
RemoteIp = batch.RemoteIp,
});
}
await db.SaveChangesAsync(cancelToken);
}
}
Rules:
- Unique index on
EntryId. The browser may send an entry twice (the server stored it, but the reply was lost). Ignore duplicates instead of failing. Entries from library 2.0 have noEntryId. - Throw only for real failures (database down). The browser keeps the batch and retries. Entries you don't want, skip silently: a rejected batch is retried and blocks that browser's queue.
- Don't trust
Context,PayloadorTimestampfor identity.AuditLogBatchcarries the server's facts:UserId,User,ConnectionId,RemoteIp,ReceivedAt. UserIdover HTTP comes from the claims. The hub uses SignalR's user identifier, so a customIUserIdProviderapplies there. The HTTP endpoint has no SignalR connection and reads theNameIdentifierorsubclaim. With a custom provider, read the id fromUserif both must match.
Sending commands
Inject IAuditCommandSender:
app.MapPost("/admin/users/{userId}/audit/pull", async (string userId, IAuditCommandSender commands) =>
{
var commandId = await commands.PullAsync(userId);
return Results.Ok(new { commandId });
}).RequireAuthorization("admin");
| Method | Browser does |
|---|---|
PullAsync(userId) |
sends queued entries now |
ClearAsync(userId) |
deletes all local entries, including unsent ones |
PauseAsync / ResumeAsync |
stops / restarts sending (keeps logging locally) |
SetLevelAsync(userId, "warn") |
sets the minimum level, kept across reloads; null resets it |
SendToUserAsync / SendToConnectionAsync / SendToAllAsync |
any command, including your own types |
Commands go to every tab of the user and need the hub (SignalR). userId is SignalR's user identifier,
by default the NameIdentifier claim.
To learn when a browser ran a command, register an IAuditCommandAckHandler. Each tab acks on its own;
no ack means unknown (the tab may have closed), not failure.
License
Apache-2.0
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.0 | 0 | 12/4/2026 |