Base solution for your next web application
Open Closed

How to Add new Column in the AbpAuditLog #10185


User avatar
0
mahendra created
  • What is your product version?
  • 5.2
  • What is your product type (Angular or MVC)?
  • Angular
  • What is product framework type (.net framework or .net core)?
  • .netCore
  • What is ABP Framework version?
  • 5.2

Hi Expert,

I have to add new Column in AbpAuditLog. like APIRequest, APIResponse in the API var auditInfo = new AuditInfo { TenantId = AbpSession.TenantId, UserId = AbpSession.UserId, ImpersonatorUserId = AbpSession.ImpersonatorUserId, ImpersonatorTenantId = AbpSession.ImpersonatorTenantId, ServiceName = typeObject != null ? typeObject.FullName : "", MethodName = methodName, Parameters = paramerters, ExecutionTime = DateTime.Now, CustomData = paramerters, // New Column that needs to be added APIResponse = "", APIRequest ="" }

 How can we modify the table. Can you guides us step step process for this.
 I want to use this in my method. There is two more property in this 
 1) Browserinfo
 2) ClientIPAddress
 How we can add value in thes property dynamically. Called in API Method.
 
3) Is there any way by which we can Change the datatype of the column (CustomData) ?
 
 Any help will be appreciated.
 
 Thanks 
 Ashish Mohan 

8 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can extend the AuditLogs table as explained in https://docs.aspnetzero.com/en/aspnet-core-angular/latest/Extending-Existing-Entities-Core-Angular.

    Then, you can create your own AuditingStore as explained in https://support.aspnetzero.com/QA/Questions/8032/Change-Auditing-data-store#answer-71ed56d9-1e95-fcdd-a27f-39f18e71b3c8 and save your extended AuditLog entity.

  • User Avatar
    0
    mahendra created

    Hi ismcagdas,

    Thansks for your reply.

    As per the first link we can see the User.cs in the visual studio solution explorer and we can add the fields in AbpUsers table. However, we are unable to find the entity (i.e class) in the visual studio solution explorer for AbpAuditLogs Table.

    Could you please help us how can we add new column in AbpAuditLogs Table.

    Any help would be much appreciated.

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry, you need to follow the second part, see https://docs.aspnetzero.com/en/aspnet-core-angular/latest/Extending-Existing-Entities-Core-Angular#extending-non-abstract-entities

    Basically you need to do something like this;

    public class MyAuditLog : AuditLog
    {
       //...other fields
    
       public string ExtraFiekld { get; set; }
            
       //...other fields
       
    }
    
    

    And add this entity to your DbContext;

    public virtual DbSet<MyAuditLog> MyAuditLogs { get; set; }
    
  • User Avatar
    0
    mahendra created

    Thanks for your quick response.

    We are able to create the new field in AbpAuditLogs table. One thing I have noticed that it also creates one extra field "Discriminator".

    We are not able to save the record using auditingHelper.SaveAsync(auditInfo) as it takes the parameter AuditInfo and AuditInfo does not have the Newly added fields. Could you please let us know how can we save the new record (with new fields) through auditingHelper.SaveAsync method.

    Although, we have successfully inserted the record in Auditlog using the IRepository<OEAuditLog, long> auditLogRepository, with the Additional field which we have created in AbpAuditLogs table. Please see the below screenshot for your reference.

    Now when we are fetching the records using the repository IRepository<AuditLog, long> auditLogRepository, we are getting all the records. However, we are not getting the new fields/value which we have added in this table. Please see the below screenshot for your reference.

    .

    If we are fetching the records using the repository IRepository<OEAuditLog, long> auditLogRepository, we are getting only the records which we have inserted using the IRepository<OEAuditLog, long> auditLogRepository. We are not getting all the AbpAuditLog Table records.

    Could you please help us, how can we get the all the records with new fields data.

    Best Regards

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mahendra

    Discriminator field is added by EF Core to separete two different entities. In your case, you need to use OEAuditLog in your code. As I mentined here, you need to create your AuditingStore to save OEAuditLog for audit logs.

    If you also want to retrieve existing data when using OEAuditLog with repositories, you neede to manually update the Discriminator field of existing recods. You can also do this in a migration as well.

  • User Avatar
    0
    mahendra created

    Hi ismcagdas,

    Thanks for your response.

    Out of the two approaches you shared, the first one makes sense to me.

    I would like to implement the interface as it is explained in the shared link and instead of saving the log in mongo db, I would like to save the data in ASP.NET Zero database in my own custom OEAuditLog table. This way I will try to achieve saving all the log in single repository.

    Let me know your thoughts also, do you think it's feasible?

    I also would like to ask you. If you have similar example with the SQL Server.

    Thanks, Best Regards

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mahendra

    I also would like to ask you. If you have similar example with the SQL Server.

    If you are using SQL Server in your app, you can directly use a generic repository to save your OEAuditLog entity.

  • User Avatar
    0
    mahendra created

    ok.Thanks for your help