Base solution for your next web application
Open Closed

Extend Audit Log #4452


User avatar
0
verrrrrrrrrrrrrdi created

Hi,

How to add custom field to auditLog and save the custom value ? can you share a step by step solution ?


10 Answer(s)
  • User Avatar
    0
    alper created
    Support Team
    • Extend AuditLog entity (inherit).
    • Replace IAuditingStore with your own implementation to save extended auditlog entity.
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    <cite>alper: </cite>

    • Extend AuditLog entity (inherit).
    • Replace IAuditingStore with your own implementation to save extended auditlog entity.

    can you share a code snippet? i have problem when extending AuditInfo. Do you have sample code to save auditInfo to audit log with extending property ?

  • User Avatar
    0
    aaron created
    Support Team

    Can you describe your use case?

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @aaron ,

    1. Extend auditlog entity. add ApplicationName Property
    2. want to save auditlog to entity with applicationName value

    Extend AuditingStore

    using System;
    using System.Threading.Tasks;
    using Abp.Auditing;
    using Abp.Domain.Repositories;
    
    namespace CompanyName.ProjectName.Extended.Auditing
    {
        public class ExtendedAuditStore : AuditingStore
        {
            public ExtendedAuditStore(IRepository<AuditLog, long> auditLogRepository) : base(auditLogRepository)
            {
            }
    
            public override Task SaveAsync(ExtendedAuditInfo auditInfo)
            {
                var result = base.SaveAsync(auditInfo);
    
                return result;
            }   
        }
    }
    

    Extend AuditInfo

    using System;
    using Abp.Auditing;
    
    namespace CompanyName.ProjectName.Extended.Auditing
    {
        public class ExtendedAuditInfo : AuditInfo
        {
            public string ApplicationName { get; set; }
        }
    }
    

    Then i got error "no suitable method found to override" because i using ExtendedAuditInfo as parameter in saveasync. can you help me ?

    this link is has a same case wih me: #143@9631572b-914f-48a0-b397-5fc4249f15fb

  • User Avatar
    0
    alirizaadiyahsi created

    Hi @Verrrrrrrrrrrrrdi, you can use CustomData field for your additional fields. And also you can use ClientName for your ApplicationName filed.

    Unfortunately you can't use existing AuditingStore to save extended AuditLog entity. Even if you pass ExtendedAuditLog entity, it is converted to new AuditEntity in AuditingStore. Because there are following lines in AuditingStore in ABP:

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/bc266e3503fcb2825684ec829cc356815f94f397/src/Abp.Zero.Common/Auditing/AuditingStore.cs">https://github.com/aspnetboilerplate/as ... ngStore.cs</a>

    public virtual Task SaveAsync(AuditInfo auditInfo)
            {
                return _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
            }
    

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/bc266e3503fcb2825684ec829cc356815f94f397/src/Abp.Zero.Common/Auditing/AuditLog.cs">https://github.com/aspnetboilerplate/as ... uditLog.cs</a>

    public static AuditLog CreateFromAuditInfo(AuditInfo auditInfo)
            {
                var exceptionMessage = auditInfo.Exception != null ? auditInfo.Exception.ToString() : null;
                return new AuditLog
                       {
                           TenantId = auditInfo.TenantId,
                           UserId = auditInfo.UserId,
                           ServiceName = auditInfo.ServiceName.TruncateWithPostfix(MaxServiceNameLength),
                           MethodName = auditInfo.MethodName.TruncateWithPostfix(MaxMethodNameLength),
                           Parameters = auditInfo.Parameters.TruncateWithPostfix(MaxParametersLength),
                           ExecutionTime = auditInfo.ExecutionTime,
                           ExecutionDuration = auditInfo.ExecutionDuration,
                           ClientIpAddress = auditInfo.ClientIpAddress.TruncateWithPostfix(MaxClientIpAddressLength),
                           ClientName = auditInfo.ClientName.TruncateWithPostfix(MaxClientNameLength),
                           BrowserInfo = auditInfo.BrowserInfo.TruncateWithPostfix(MaxBrowserInfoLength),
                           Exception = exceptionMessage.TruncateWithPostfix(MaxExceptionLength),
                           ImpersonatorUserId = auditInfo.ImpersonatorUserId,
                           ImpersonatorTenantId = auditInfo.ImpersonatorTenantId,
                           CustomData = auditInfo.CustomData.TruncateWithPostfix(MaxCustomDataLength)
                       };
            }
    
  • User Avatar
    0
    alper created
    Support Team

    <cite>Verrrrrrrrrrrrrdi: </cite> Hi @aaron ,

    1. Extend auditlog entity. add ApplicationName Property
    2. want to save auditlog to entity with applicationName value

    Extend AuditingStore

    using System;
    using System.Threading.Tasks;
    using Abp.Auditing;
    using Abp.Domain.Repositories;
    
    namespace CompanyName.ProjectName.Extended.Auditing
    {
       public class ExtendedAuditStore : AuditingStore
       {
           public ExtendedAuditStore(IRepository<AuditLog, long> auditLogRepository) : base(auditLogRepository)
           {
           }
    
           public override Task SaveAsync(ExtendedAuditInfo auditInfo)
           {
               var result = base.SaveAsync(auditInfo);
    
               return result;
           }   
       }
    }
    

    Extend AuditInfo

    using System;
    using Abp.Auditing;
    
    namespace CompanyName.ProjectName.Extended.Auditing
    {
       public class ExtendedAuditInfo : AuditInfo
       {
           public string ApplicationName { get; set; }
       }
    }
    

    Then i got error "no suitable method found to override" because i using ExtendedAuditInfo as parameter in saveasync. can you help me ?

    this link is has a same case wih me: #143@9631572b-914f-48a0-b397-5fc4249f15fb

    Inject IRepository<ExtendedAuditInfo, long> auditLogRepository

    public ExtendedAuditStore(IRepository<ExtendedAuditInfo , long> auditLogRepository) : base(auditLogRepository)
            {
            }
        public override Task SaveAsync(ExtendedAuditInfo auditInfo)
            {
               // var result = base.SaveAsync(auditInfo);
           ---> do your custom insert <---
    
    
                return result;
            }
    
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @alper,

    I got this error message when implementing your code

    cannot be used as type parameter ‘TEntity’ in the generic type or method ‘IRepository<TEntity, PrimaryKey>’. There is no implicit reference conversion from ‘ExtendedAuditInfo’ to ‘Abp.Domain.Entities.IEntity<long>’

  • User Avatar
    0
    alper created
    Support Team

    can you show what you did in SaveAsync method

    public override Task SaveAsync(ExtendedAuditInfo auditInfo)
            {      
           ---> do your custom insert <--- 
            }
    
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    <cite>alper: </cite> can you show what you did in SaveAsync method

    public override Task SaveAsync(ExtendedAuditInfo auditInfo)
           {      
          ---> do your custom insert <--- 
           }
    
    using System;
    using System.Threading.Tasks;
    using Abp.Auditing;
    using Abp.Domain.Repositories;
    
    namespace CompanyName.ProjectName.Extended.Auditing
    {
        public class ExtendedAuditStore : AuditingStore
        {
            public ExtendedAuditStore(IRepository<ExtendedAuditInfo , long> auditLogRepository) : base(auditLogRepository) //Error : cannot be used as type parameter ‘TEntity’ in the generic type or method ‘IRepository<TEntity, PrimaryKey>’. There is no implicit reference conversion from ‘ExtendedAuditInfo’ to ‘Abp.Domain.Entities.IEntity<long>’
            {
            }
        public override Task SaveAsync(ExtendedAuditInfo auditInfo) //Error : no suitable method found to override" because i using ExtendedAuditInfo as parameter in saveasync. 
            {
               var result = base.SaveAsync(auditInfo);
        
                return result;
            }   
        }
    }
    
  • User Avatar
    0
    aaron created
    Support Team

    It should be IRepository<ExtendedAuditLog, long> since AuditInfo is not an entity.

    If you don't want to use CustomData (as suggested by @alirizaadiyahsi), you have to do your custom insert (as suggested by @alper). You should override Task SaveAsync(AuditInfo auditInfo) to save your ExtendedAuditLog, but NOT call base.SaveAsync(auditInfo).