Audit.NET.Firestore
31.0.1
dotnet add package Audit.NET.Firestore --version 31.0.1
NuGet\Install-Package Audit.NET.Firestore -Version 31.0.1
<PackageReference Include="Audit.NET.Firestore" Version="31.0.1" />
<PackageVersion Include="Audit.NET.Firestore" Version="31.0.1" />
<PackageReference Include="Audit.NET.Firestore" />
paket add Audit.NET.Firestore --version 31.0.1
#r "nuget: Audit.NET.Firestore, 31.0.1"
#:package Audit.NET.Firestore@31.0.1
#addin nuget:?package=Audit.NET.Firestore&version=31.0.1
#tool nuget:?package=Audit.NET.Firestore&version=31.0.1
Audit.NET.Firestore
Google Cloud Firestore provider for Audit.NET library. (An extensible framework to audit executing operations in .NET).
Store the audit events in a Google Cloud Firestore database as documents in a collection.
Install
NuGet Package
PM> Install-Package Audit.NET.Firestore
Usage
Please see the Audit.NET Readme
Configuration
Set the static Audit.Core.Configuration.DataProvider
property to set the Firestore data provider, or call the UseFirestore
method on the fluent configuration. This should be done before any AuditScope
creation, i.e. during application startup.
Basic Configuration
Using the fluent configuration API:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.Collection("AuditEvents"));
Or directly assigning the data provider:
Audit.Core.Configuration.DataProvider = new Audit.Firestore.Providers.FirestoreDataProvider()
{
ProjectId = "your-project-id",
Collection = "AuditEvents"
};
Authentication
The Firestore provider supports several authentication methods:
Default Application Credentials
If running on Google Cloud Platform or with properly configured environment variables:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.Collection("AuditEvents"));
Service Account Key File
Using a JSON credentials file:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.CredentialsFromFile("path/to/credentials.json")
.Collection("AuditEvents"));
Service Account JSON String
Using credentials as a JSON string:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.CredentialsFromJson(credentialsJsonString)
.Collection("AuditEvents"));
Custom FirestoreDb Instance
Using a pre-configured FirestoreDb instance:
var firestoreDb = FirestoreDb.Create("your-project-id");
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.FirestoreDb(firestoreDb)
.Collection("AuditEvents"));
Provider Options
- ProjectId: The Google Cloud project ID (required unless using custom FirestoreDb).
- Database: The Firestore database name. Default is "(default)".
- Collection: The Firestore collection name for storing audit events. Can be a fixed string or a function of the audit event.
- CredentialsFilePath: Path to the service account credentials JSON file.
- CredentialsJson: Service account credentials as a JSON string.
- FirestoreDb: A custom pre-configured FirestoreDb instance.
- IdBuilder: A function that returns the document ID to use for a given audit event. By default, Firestore generates the ID automatically.
- SanitizeFieldNames: Whether to sanitize field names by replacing dots with underscores. Default is false.
- ExcludeNullValues: Whether to exclude null values from the stored audit event data. Default is false.
Advanced Configuration
Dynamic Collection Names
You can configure collection names based on the audit event:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.Collection(auditEvent => $"Audit_{auditEvent.EventType}")
.IdBuilder(auditEvent => $"{auditEvent.EventType}_{DateTime.UtcNow.Ticks}"));
Multiple Databases
Firestore supports multiple databases per project:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.Database("audit-database")
.Collection("AuditEvents"));
Querying Events
The Firestore data provider includes methods to retrieve and query audit events.
Get an Event by ID
var event = firestoreDataProvider.GetEvent("eventId");
Query Events (In-Memory)
The QueryEvents()
method returns an IQueryable that loads all events into memory:
var events = firestoreDataProvider.QueryEvents()
.Where(ev => ev.EventType == "Login")
.OrderByDescending(ev => ev.StartDate)
.Take(10)
.ToList();
Query Events (Firestore Native)
For better performance with large collections, use the native Firestore query methods:
// Query with Firestore filters
var events = await firestoreDataProvider.QueryEventsAsync(query => query
.WhereEqualTo("EventType", "Login")
.WhereGreaterThan("StartDate", DateTime.UtcNow.AddDays(-7))
.OrderByDescending("StartDate")
.Limit(10));
Access Native Firestore Collection
You can access the native Firestore collection reference for advanced operations:
var collection = firestoreDataProvider.GetFirestoreCollection();
var query = collection.WhereEqualTo("Environment.UserName", "admin");
var snapshot = await query.GetSnapshotAsync();
Field Name Restrictions
Firestore has restrictions on field names (cannot contain dots). You can enable automatic sanitization to replace dots with underscores:
Audit.Core.Configuration.Setup()
.UseFirestore(config => config
.ProjectId("your-project-id")
.SanitizeFieldNames(true)); // Automatically replace dots with underscores
Output Sample
Audit events are stored as Firestore documents with the following structure:
{
"_timestamp": "2024-01-10T10:30:45Z",
"EventType": "Order:Update",
"Environment": {
"UserName": "john.doe",
"MachineName": "WORKSTATION01",
"DomainName": "CORPORATE",
"CallingMethodName": "OrderService.UpdateOrder",
"Culture": "en-US"
},
"StartDate": "2024-01-10T10:30:45.123Z",
"EndDate": "2024-01-10T10:30:45.789Z",
"Duration": 666,
"Target": {
"Type": "Order",
"Old": {
"OrderId": 12345,
"Status": "Pending",
"Total": 150.00
},
"New": {
"OrderId": 12345,
"Status": "Approved",
"Total": 150.00
}
},
"CustomFields": {
"ApprovedBy": "manager@company.com",
"ApprovalReason": "Valid payment method"
}
}
Performance Considerations
Document Size: Firestore documents have a maximum size of 1MB. Large audit events may exceed this limit.
Indexing: Create composite indexes for fields you frequently query together.
Collection Size: For high-volume auditing, consider:
- Using dynamic collection names to partition data
- Implementing a retention policy to delete old events
- Using batch operations for bulk inserts
Field Names: Field name sanitization (dots to underscores) adds a small overhead. Enable it only if your field names contain dots.
Connection Testing
You can test the Firestore connection:
var provider = new FirestoreDataProvider()
{
ProjectId = "your-project-id"
};
await provider.TestConnectionAsync();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Audit.NET (>= 31.0.1)
- Google.Cloud.Firestore (>= 3.10.0)
-
.NETStandard 2.0
- Audit.NET (>= 31.0.1)
- Google.Cloud.Firestore (>= 3.10.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.