Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Custom Audit Log Entries not saving/creating #9830


User avatar
0
admin@SYNTAQ created

Prerequisites

  • Ver 8.8
  • MVC
  • .net Core 3.1

Hi,

I am trying to creaste custom Audit Log entries from the Application API level. I have a custom Audit class as below. When I try to call and save to the Audit log no audit data si created and no errors are thrown / reported.

Any suggestions?

Sample Call to Audit Class

AuditInfo info = new AuditInfo()
			{
				BrowserInfo = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 Edg/86.0.622.38",
				ClientIpAddress = "::1",
				ClientName = "",
				CustomData = "",
				Exception = null,
				MethodName = "GetAll",
				Parameters = "{}",
				ServiceName = "Syntaq.Web.Controllers.Forms",
				TenantId = AbpSession.TenantId,
				UserId = AbpSession.UserId
			};

			AuditSyntaqLogStore log = new AuditSyntaqLogStore(_auditLogRepository);
			await log.SaveAsync(info);

Custom Class

public class AuditSyntaqLogStore : IAuditingStore, ITransientDependency
	{
		private readonly IIocResolver _iocResolver;
		private readonly IRepository<AuditLog, long> _auditLogRepository;

		/// <summary>
		/// Creates  a new <see cref="AuditingStore"/>.
		/// </summary>
		public AuditSyntaqLogStore(
		IRepository<AuditLog, long> auditLogRepository
		)
		{
			_auditLogRepository = auditLogRepository;
		}

        public void Save(AuditInfo auditInfo)
        {
			_auditLogRepository.Insert(AuditLog.CreateFromAuditInfo(auditInfo));
		}

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



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

    Hi,

    Could you also share where do you call await log.SaveAsync(info); ? Is it in an application service method ? Could you share the class which executes await log.SaveAsync(info); ?

    This is happening probably unitOfWork's complete method is not called.

  • User Avatar
    0
    admin@SYNTAQ created

    Hi,

    This is being called from an application service metohd. I have explicitly wrapped a nuit of work around the audit logging and still not audit record is committed.

    using (var unitOfWork = _unitOfWorkManager.Begin()) { AuditInfo info = new AuditInfo() { BrowserInfo = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 Edg/86.0.622.38", ClientIpAddress = "::1", ClientName = "", CustomData = "", Exception = null, MethodName = "GetAll", Parameters = "{}", ServiceName = "Syntaq.Web.Controllers.Forms", TenantId = AbpSession.TenantId, UserId = AbpSession.UserId }; _auditLogRepository.Save(info); unitOfWork.Complete(); }

  • User Avatar
    0
    zony created
    Support Team

    Hi admin@syntaq, It works in my project.

    code:

    public class AuditSyntaqLogStore : IAuditingStore, ITransientDependency
    {
        private readonly IIocResolver _iocResolver;
        private readonly IRepository<AuditLog, long> _auditLogRepository;
    
        public AuditSyntaqLogStore(IRepository<AuditLog, long> auditLogRepository)
        {
            _auditLogRepository = auditLogRepository;
        }
    
        public Task SaveAsync(AuditInfo auditInfo)
        {
            return _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
        }
    
        public void Save(AuditInfo auditInfo)
        {
            _auditLogRepository.Insert(AuditLog.CreateFromAuditInfo(auditInfo));
        }
    }
    
    
    public class DemoAppService : ApplicationService, IDemoAppService
    {
        private readonly IRepository<AuditLog, long> _auditLogRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
    
        public DemoAppService(IRepository<AuditLog, long> auditLogRepository,
            IUnitOfWorkManager unitOfWorkManager)
        {
            _repository = repository;
            _auditLogRepository = auditLogRepository;
            _unitOfWorkManager = unitOfWorkManager;
        }
    
        public async Task TestAuditStoreAsync()
        {
            using (var uow = _unitOfWorkManager.Begin())
            {
                AuditInfo info = new AuditInfo()
                {
                    BrowserInfo = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 Edg/86.0.622.38",
                    ClientIpAddress = "::1",
                    ClientName = "",
                    CustomData = "",
                    Exception = null,
                    MethodName = "GetAll",
                    Parameters = "{}",
                    ServiceName = "Syntaq.Web.Controllers.Forms",
                    TenantId = AbpSession.TenantId,
                    UserId = AbpSession.UserId
                };
    
                AuditSyntaqLogStore log = new AuditSyntaqLogStore(_auditLogRepository);
                await log.SaveAsync(info);
                uow.Complete();
            }
        }
    }
    
  • User Avatar
    0
    admin@SYNTAQ created

    Thanks,

    This is now workign from the application service level. I don't know why it did not before.

    One last issue as it is not working from a Class in the Syntaq.Falcon.Core project. The class inhertis from the FalconDomainServiceBase class.

    Any suggestions? The code is exactly as the code in the Application service class that works.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @admin@syntaq

    Domain services are not UnitOfWork by default. So, you can add [UnitOfWork] to related method on your service and it should work.