User Information on Entities

We just released Radzen 2.12.1 which has the needed changes. Also we released the AuditTrail sample application.

There are two custom files that you need to replicate in your application - server\Startup.Audit.cs and server\data\AuditTrailDbContext.Audit.cs (you should create a partial class for the DB context that Radzen has generated for your database).

The actual logging is done in the Audit method. You can rename the properties that the sample is using to match your existing DB schema, perform additional logging etc:

public static void Audit(EntityEntry entityEntry, string userName)
{
     var type = entityEntry.Entity.GetType();
     if (entityEntry.State == EntityState.Added)
     {
          SetProperty(type, entityEntry, "CreatedBy", userName);
          SetProperty(type, entityEntry, "CreatedAt", DateTime.UtcNow);
     }
     else if (entityEntry.State == EntityState.Modified)
     {
          SetProperty(type, entityEntry, "UpdatedBy", userName);
          SetProperty(type, entityEntry, "UpdatedAt", DateTime.UtcNow);
     }
}
4 Likes