Base solution for your next web application
Open Closed

How to disable audit logs except exception log as they are effecting performance? #7512


User avatar
0
razkhan78 created

Hello, We are using AspNetZero (Paid) Product version --> 4.0 Product type --> MVC Product framework type --> .net core

We want to disable logging each and every event happening in system. We only want to log exceptions. Reason: It seems to be affecting system performance significantly.

Please do let us know if there is a way to do so via some configuration changes or any other way?


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

    Hi,

    You can implement your own AuditingStore similar to https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Zero.Common/Auditing/AuditingStore.cs and save audit log only if auditInfo.Exception is not null.

  • User Avatar
    0
    cmthomps created

    As @ismcagdas suggested, you can implement your own AuditStore. What we did was create an admin setting that we could use to log only errors or log all events This is what our implementation looks like:

    
        public class SmcAuditStore : AuditingStore
        {
    
            public ILogger<AuditingStore> Logger { get; set; }
    
            private readonly IRepository<AuditLog, long> _auditLogRepository;
            private readonly ISettingManager _settingManager;
    
            public SmcAuditStore(IRepository<AuditLog, long> auditLogRepository, ISettingManager settingManager) : base(auditLogRepository)
            {
                _auditLogRepository = auditLogRepository;
                _settingManager = settingManager;
            }
    
            public override async Task SaveAsync(AuditInfo auditInfo)
            {
                AuditLog auditLog = new AuditLog();
    
                bool logErrorsOnly = await _settingManager.GetSettingValueAsync<bool>(AppSettings.Logging.LogOnErrorsOnly);
    
                var exceptionMessage = auditInfo.Exception != null ? auditInfo.Exception.ToString() : null;
    
                if ((logErrorsOnly && exceptionMessage != null) || !logErrorsOnly)
                {
    
                    auditLog = await _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
                }
    
    
            }
    
        }
    

    You can replace the default auditstore by replacing it in the Core Module PreInitialize:

    Configuration.ReplaceService<IAuditingStore, SmcAuditStore>();
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @cmthomps for sharing this. Such a setting would be good for AspNet Zero I guess :).

  • User Avatar
    1
    cmthomps created

    @ismcagdas - It would be a nice feature!