Reference

AddAudit

Package Trax.Api.GraphQL.AuditNamespace Trax.Api.GraphQL.AuditSource TraxGraphQLBuilderAuditExtensions.cs

NO WARRANTY. Trax auth is plumbing, not a security product. You are solely responsible for securing systems that use it. See API Security.

Extension method on TraxGraphQLBuilder, declared on TraxGraphQLBuilderAuditExtensions, that registers the GraphQL audit pipeline: a bounded channel, a background writer, a HotChocolate diagnostic listener, the sink TSink and a redactor.

Signature

public static TraxGraphQLBuilder AddAudit<TSink>(
    this TraxGraphQLBuilder builder,
    Action<TraxAuditOptions>? configure = null
)
    where TSink : class, ITraxAuditSink;

Parameters

ParameterTypeDescription
TSinktype argumentThe ITraxAuditSink that receives the entries.
builderTraxGraphQLBuilderThe builder AddTraxGraphQL passes to its callback.
configureAction<TraxAuditOptions>?Sets TraxAuditOptions. Omitted, every option keeps its default.

Returns

The same TraxGraphQLBuilder, for chaining.

Behavior

Every GraphQL request is recorded as a TraxAuditEntry, including one a gate refuses: a refused train, query model or operations call is an entry with Success = false and the caller's scheme-qualified PrincipalId. A socket refused at connection_init and a failed event of an accepted subscription are recorded too. The request thread never waits on the sink.

The options are validated when the host starts. A value out of range refuses startup with an OptionsValidationException naming the option; the rules are on TraxAuditOptions.

Throws ArgumentNullException when builder is null.

Registered services

ServiceLifetimePurpose
TraxAuditChannelSingletonBounded channel between the listener and the writer. Counts dropped entries.
TraxGraphQLAuditListenerSingleton, added to the schema as a diagnostic event listenerBuilds an entry for each request and offers it to the channel.
TraxAuditWriterHosted serviceDrains the channel in batches and calls the sink, with retries. On shutdown it writes every accepted entry within the host's shutdown timeout.
ITraxAuditSink -> TSinkScopedThe destination. It is resolved from a new scope for each write attempt, so an IDbContextFactory<T> or a scoped DbContext both work.
ITraxAuditRedactor -> DefaultAuditRedactorSingleton, only when none is registeredRecords no variables. Register your own ITraxAuditRedactor to record them.
ISocketRefusalObserver -> TraxSocketRefusalAuditSingletonRecords a socket refused at connection_init, which never reaches the listener.
IHttpContextAccessorSingletonThe listener reads HttpContext.User.
TimeProviderSingleton, only when none is registeredTimeProvider.System.
IValidateOptions<TraxAuditOptions>Singleton, with ValidateOnStartRefuses startup when an option is out of range.
Disclaimer hosted serviceSingleton, registered onceLogs the NO WARRANTY notice at startup.

Example

services.AddTraxGraphQL(graphql =>
    graphql.AddAudit<AuditTableSink>(opts =>
    {
        opts.ChannelCapacity = 10_000;
        opts.BatchSize = 50;
        opts.FlushInterval = TimeSpan.FromMilliseconds(500);
    })
);

See also