Auditing
Have you ever needed to know who changed a critical piece of data in your application, or when a specific record was deleted? Tracking changes to your data is crucial for security, debugging, and often, for legal or compliance reasons. This process is called auditing, and while it’s incredibly important, building a robust auditing system from scratch can be a lot of work.
Fortunately, SliceFlow comes with a powerful and automatic auditing system built right in. It’s designed to give you a complete history of every change made to your important data, without you having to write any complex tracking code yourself.
How Does It Work?
At its core, SliceFlow’s auditing system is like a vigilant security camera for your database. It uses a clever technique to automatically intercept any changes being made—whether a new piece of data is being created, an existing one is updated, or something is deleted.
For every change it detects, it records a detailed entry in an “audit log,” capturing:
- Who made the change (linking to the logged-in user).
- When the change was made (with a precise timestamp).
- What was changed (including the original values and the new values).
This gives you a clear, chronological history of your data, which can be invaluable for troubleshooting issues or understanding how your application is being used.
Making Your Data Auditable
To tell SliceFlow that you want to track changes for a specific type of data (like a Product
or an Order
), you simply need to implement the IAuditableEntity
interface.
Here’s an example of how you would do this for a Product
entity:
public class Product : IAuditableEntity{ public Guid Id { get; set; } public string Name { get; set; } public decimal Price { get; set; }
// Auditing properties public DateTime CreatedAtUtc { get; set; } public DateTime? UpdatedAtUtc { get; set; } public string CreatedBy { get; set; } public string? UpdatedBy { get; set; }}
By adding IAuditableEntity
, you’re essentially “opting in” to the auditing system for this entity. From this point on, SliceFlow will automatically manage the auditing properties for you:
- When a new product is created,
CreatedAtUtc
andCreatedBy
will be automatically populated. - When a product is updated,
UpdatedAtUtc
andUpdatedBy
will be filled in.
You never have to set these properties yourself; the system takes care of it all behind the scenes.
The Audit Trail
All the change history is stored in a dedicated AuditLog
table in your database. Each time an auditable entity is changed, a new record is added to this table. This log contains all the details you need to understand what happened, including:
- The name of the entity that was changed.
- The type of change (Create, Update, or Delete).
- The user who made the change.
- The exact date and time of the change.
- A snapshot of the data before the change (
OldValues
). - A snapshot of the data after the change (
NewValues
).
This detailed information is stored efficiently, so you can easily query it later to reconstruct the history of any piece of data.
Querying the Audit History
Since the audit log is just another table in your database, you can easily query it to find the information you need. For example, you could retrieve all the changes made to a specific product:
var productAudits = context.Set<AuditLog>() .Where(a => a.EntityName == "Product" && a.PrimaryKey == productId.ToString()) .OrderByDescending(a => a.DateUtc) .ToList();
Or, you could find all the recent changes made by a particular user:
var userChanges = context.Set<AuditLog>() .Where(a => a.UserId == userId) .OrderByDescending(a => a.DateUtc) .Take(50) .ToList();
Best Practices
- Be selective: Enable auditing only on the entities where change tracking is truly important to avoid unnecessary data storage.
- Trust the system: Don’t try to manually set the
CreatedAtUtc
or other audit properties; let SliceFlow’s automatic system handle it. - Plan for storage: Audit logs can grow quite large over time. For applications with a lot of activity, you might want to consider an archiving strategy for older logs.
By leveraging SliceFlow’s built-in auditing, you get a comprehensive, zero-effort system for tracking data changes, giving you greater insight, security, and control over your application.