Open Closed

Abp Auditing log on DB #242


0
nobruds created

Hello,

I was looking into Module zero, to see how can I use auditing logs to insert info on my DB.

But if I follow that example, i wont be able to use the AuditingInterceptor class from Abp framework and AuditedAttribute right ? because wont save on DB

Or there's a way around that, instead I call the "AuditingStore.Save" on Intercept method, I call my db repository ?

Thanks


5 Answer(s)
  • 0
    hikalkan created
    Support Team

    It saves audit logs automatically (see docs: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Audit-Logging">http://www.aspnetboilerplate.com/Pages/ ... it-Logging</a>).

    Do you want that other class methods (than app service methods) write aufit logs. Document have a related section: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Audit-Logging#DocConfig">http://www.aspnetboilerplate.com/Pages/ ... #DocConfig</a>

    Do you want to insert a log manually? Just inject and use IAuditingStore.

  • 0
    nobruds created

    Hi,

    Yes, I saw those docs, but want to save on my database.

    on Abp framework I saw 2 examples where it uses the AuditingStore.Save, but both is instances of "SimpleLogAuditingStore", that saves only on a .txt file right ? Its not clear to me how to use it.

    If I create a custom class like on Abp.Zero "public class AuditingStore : IAuditingStore, ITransientDependency" how will I Fill my AuditInfo object ? when that save is called? I don't see that on abp.zero sample.

    sorry for the trouble Thanks.

    <cite>hikalkan: </cite> It saves audit logs automatically (see docs: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Audit-Logging">http://www.aspnetboilerplate.com/Pages/ ... it-Logging</a>).

    Do you want that other class methods (than app service methods) write aufit logs. Document have a related section: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Audit-Logging#DocConfig">http://www.aspnetboilerplate.com/Pages/ ... #DocConfig</a>

    Do you want to insert a log manually? Just inject and use IAuditingStore.

  • 0
    hikalkan created
    Support Team

    Hi,

    OK, I understood now.

    You can implement IAuditingStore as like in module-zero and connect to any database and save it, no problem.

    When it's called? It's automatically called by ABP using intercepting. As default, ABP writes audit logs for every application service method call and MVC Controller method call. If these are not enough for you, you can customize it. See doc: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Audit-Logging#DocConfig">http://www.aspnetboilerplate.com/Pages/ ... #DocConfig</a> Use Configuration.Auditing.Selectors.Add(...) to select types which should be logged by convention. Or use [Audited] attributes to classes/methods you want. Even you can inject and use IAuditingStore directly but this should not be needed mostly.

  • 0
    nobruds created

    Hi I think I got it too. Just to clear out.

    So if I do like module zero, the audit log won't be saved automatically by abp Interceptor right?

    Because class "AuditingInterceptor" and method "Intercept", will write the log like this:

    SimpleLogAuditingStore.Save
    Logger.Info(auditInfo.ToString());
    

    So, my scenario will be this:

    My log store
    public class AuditingStore : IAuditingStore, ITransientDependency
        {
            private readonly IRepository<AuditLog, long> _auditLogRepository;
    
            /// <summary>
            /// Creates  a new <see cref="AuditingStore"/>.
            /// </summary>
            public AuditingStore(IRepository<AuditLog, long> auditLogRepository)
            {
                _auditLogRepository = auditLogRepository;
            }
    
            public Task SaveAsync(AuditInfo auditInfo)
            {
                return _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
            }
        }
    
    my app service
    public class GerenciadorBoletadorService : IGerenciadorBoletadorService
        {
            private readonly IGerenciadorBoletadorRepository _gerenciadorBoletadorRepository;
            private readonly IAuditingStore _auditStore;
    
            public GerenciadorBoletadorService(IGerenciadorBoletadorRepository gerenciadorBoletadorRepository, IAuditingStore auditingStore)
            {
                _gerenciadorBoletadorRepository = gerenciadorBoletadorRepository;
                _auditStore = auditingStore;
            }
    
            public List<TemplateObjeto> GetTemplateObjetos(int statusOperacaoId, int sistemaLegadoId, int perfilId)
            {
                List<TemplateObjeto> result = null;
    
                try
                {
                    result = _gerenciadorBoletadorRepository.GetTemplateObjetos(statusOperacaoId, sistemaLegadoId, perfilId);
    
                    _auditStore.SaveAsync(new AuditInfo());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
                return result;
            }
    }
    

    Note: If done like that I can't use the audit attributes or interceptors.

  • 0
    hikalkan created
    Support Team

    Hi,

    So if I do like module zero, the audit log won't be saved automatically by abp Interceptor right?

    No, it will be saved. ABP interceptor will call your store. See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Auditing/AuditingInterceptor.cs">https://github.com/aspnetboilerplate/as ... rceptor.cs</a> It uses IAuditingStore via PROPERTY INJECTION. In constructor it sets

    AuditingStore = SimpleLogAuditingStore.Instance
    

    but if you implement it, your implementation will override SimpleLogAuditingStore.