question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Old and new values are same in audit.net

See original GitHub issue

this is my controller

 using (var audit = AuditScope.Create("Devices:AddDevices", () => model))
 {
      \\\
 }

this is data provider

  Audit.Core.Configuration.DataProvider = new AzureTableDataProvider()
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["AuditDbConnection"].ToString(),
                TableName = "tblbateventsauditing",
                TableEntityMapper = ev => new AuditEventTableEntity(ev)
            };

but oup put gives same Old and new values

 "Target":{  
    "Type":"Devices",
    "Old":{  
      "UserId":"",
      "IvpId":0,
      "UserName":"",
      "DeviceCode":"XXXXX",
      "PurchaseDate":"2018-09-09T00:00:00",
      "PurchasePlaceId":12,
      "Active":false,
      "UpdatedDate":"0001-01-01T00:00:00",
      "DeviceStatusId":0,
      "DateofReplacement":"0001-01-01T00:00:00",
      "DateofTransfer":"0001-01-01T00:00:00",
      "RegistrationDate":"0001-01-01T00:00:00",
      "TotalRecords":0,
      "Id":18,
      "CurrentStatusId":0,
      "TRCCode":""
    },
    "New":{  
      "UserId":"",
      "IvpId":0,
      "UserName":"",
      "DeviceCode":"XXXXXX",
      "PurchaseDate":"2018-09-09T00:00:00",
      "PurchasePlaceId":12,
      "Active":false,
      "UpdatedDate":"0001-01-01T00:00:00",
      "DeviceStatusId":0,
      "DateofReplacement":"0001-01-01T00:00:00",
      "DateofTransfer":"0001-01-01T00:00:00",
      "RegistrationDate":"0001-01-01T00:00:00",
      "TotalRecords":0,
      "Id":18,
      "Createdby":"backoffice\\systemadmin",
      "Updatedby":"backoffice\\systemadmin",
      "CurrentStatusId":0,
      "TRCCode":""
    }
  },

Can you please provide solution for this. @thepirat000

Thanks !

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
BHolthuijsencommented, Jul 14, 2023

Thanks a lot for this, it works like a charm!

Now the EventEntry.Changes contain the correct OriginalValue and NewValue.

Because the logic we’re using sets the state of the item to “EntityState.Modified” before saving, all columns are included in the Changes. This is not an Audit.NET issue, but is caused by our own code.

Since the addition of ReloadDatabaseValues, we can filter the audit data before writing it to the database. (AuditLog is our generic logging table)

Audit.Core.Configuration.Setup()
            .UseEntityFramework(_ => _
            .AuditTypeMapper(t => typeof(AuditLog))
            .AuditEntityAction<AuditLog>((ev, entry, entity) =>
            {
                entity.Schema = entry.Schema;
                entity.Name = entry.Table;
                entity.Key = Convert.ToInt64(entry.PrimaryKey?.FirstOrDefault().Value);
                // set the action: I(nsert), U(pdate), D(elete)
                entity.Action = entry.Action?.Substring(0, 1);
                entity.LogDate = DateTimeOffset.Now;

                // remove all the changes where the old and new values are the same
                entry.Changes.RemoveAll(change => object.Equals(change.OriginalValue, change.NewValue));
                entity.Changes = entry.ToJson();
            })
            .IgnoreMatchedProperties(true)
            );
1reaction
thepirat000commented, Jul 6, 2023

Starting from version 21.0.2, the Audit.EntityFramework and Audit.EntityFramework.Core libraries introduce a new feature.

The setting called ReloadDatabaseValues can be globally or individually configured for a DbContext instance. It determines whether the original values of audited entities should be fetched from the database prior to saving the audit event.

Consider the following examples of update and delete operations:

using (var context = new MyAuditedContext())
{
    //context.ReloadDatabaseValues = true;

    context.Cars.Update(new Car() { Id = 123, Name = "New name" });
    await context.SaveChangesAsync();
}
using (var context = new MyAuditedContext())
{
    //context.ReloadDatabaseValues = true;

    context.Entry(new Car() { Id = 123 }).State = EntityState.Deleted;
    await context.SaveChangesAsync();
}

If the ReloadDatabaseValues is not set to true, the EF Change Tracker will lack knowledge of the original values.

Enabling the ReloadDatabaseValues setting triggers an extra database query to retrieve the original values prior to the update operation. As a result, the audit event will contain the original values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF2.2 Core 2.2 Auditing returns same old and new value ...
I am trying to implement an Audit table in my .net core (2.2) using EF 2.2 code first. My intention is to log...
Read more >
Audit Trail Implementation in ASP.NET Core with Entity ...
We will track the following with our implementation. TableName; Affected Column(s); Primary Key / Value of the Table; Old Values; New Values ......
Read more >
Entity Framework Core: History / Audit table
The OnBeforeChanges method creates a list of AuditEntry . An AuditEntry store the table name, the ids and the old and new values...
Read more >
Retrieve the history of audited data changes - Power Apps
Provides details when data changes occur for a record. Provides access to old values and new values. Returned by the following types of...
Read more >
Audit.NET
An extensible framework to audit executing operations in .NET and .NET Core. Generate audit logs with evidence for reconstruction and examination of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found