Hi,
How to add custom field to auditLog and save the custom value ? can you share a step by step solution ?
10 Answer(s)
-
0
- Extend AuditLog entity (inherit).
- Replace IAuditingStore with your own implementation to save extended auditlog entity.
-
0
<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 ?
-
0
Can you describe your use case?
-
0
Hi @aaron ,
- Extend auditlog entity. add ApplicationName Property
- 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
-
0
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) }; }
-
0
<cite>Verrrrrrrrrrrrrdi: </cite> Hi @aaron ,
- Extend auditlog entity. add ApplicationName Property
- 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; }
-
0
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>’
-
0
can you show what you did in SaveAsync method
public override Task SaveAsync(ExtendedAuditInfo auditInfo) { ---> do your custom insert <--- }
-
0
<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; } } }
-
0
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).