AuditLogLib.AspNetCore 2.1.0

dotnet add package AuditLogLib.AspNetCore --version 2.1.0
                    
NuGet\Install-Package AuditLogLib.AspNetCore -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AuditLogLib.AspNetCore" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AuditLogLib.AspNetCore" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AuditLogLib.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AuditLogLib.AspNetCore --version 2.1.0
                    
#r "nuget: AuditLogLib.AspNetCore, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AuditLogLib.AspNetCore@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AuditLogLib.AspNetCore&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AuditLogLib.AspNetCore&version=2.1.0
                    
Install as a Cake Tool

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 no EntryId.
  • 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, Payload or Timestamp for identity. AuditLogBatch carries the server's facts: UserId, User, ConnectionId, RemoteIp, ReceivedAt.
  • UserId over HTTP comes from the claims. The hub uses SignalR's user identifier, so a custom IUserIdProvider applies there. The HTTP endpoint has no SignalR connection and reads the NameIdentifier or sub claim. With a custom provider, read the id from User if 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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