Base solution for your next web application
Open Closed

USERID field is null in Abpauditlogs table #9353


User avatar
0
velu created

Hi,

We are using .netcore 3.1 and angular project.

In Abpauditlogs table the userId is always null inserted

not capturing userid in auditlogs table.

Please help us in this.

Thanks


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Can you share some auditlogs records? How can we reproduce your problem?

  • User Avatar
    0
    velu created

    Hi,

    We are using oracle database (Devart)

    Thanks

  • User Avatar
    0
    maliming created
    Support Team

    You can try replace the built-in AuditingHelper to debug and check why UserId is null.

    public override void PreInitialize()
    {
    	Configuration.ReplaceService<IAuditingHelper, MyAuditingHelper>();
    }
    
    public class MyAuditingHelper : IAuditingHelper, ITransientDependency
        {
            public ILogger Logger { get; set; }
            public IAbpSession AbpSession { get; set; }
            public IAuditingStore AuditingStore { get; set; }
    
            private readonly IAuditInfoProvider _auditInfoProvider;
            private readonly IAuditingConfiguration _configuration;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
            private readonly IAuditSerializer _auditSerializer;
    
            public MyAuditingHelper(
                IAuditInfoProvider auditInfoProvider,
                IAuditingConfiguration configuration,
                IUnitOfWorkManager unitOfWorkManager,
                IAuditSerializer auditSerializer)
            {
                _auditInfoProvider = auditInfoProvider;
                _configuration = configuration;
                _unitOfWorkManager = unitOfWorkManager;
                _auditSerializer = auditSerializer;
    
                AbpSession = NullAbpSession.Instance;
                Logger = NullLogger.Instance;
                AuditingStore = SimpleLogAuditingStore.Instance;
            }
    
            public bool ShouldSaveAudit(MethodInfo methodInfo, bool defaultValue = false)
            {
                if (!_configuration.IsEnabled)
                {
                    return false;
                }
    
                if (!_configuration.IsEnabledForAnonymousUsers && (AbpSession?.UserId == null))
                {
                    return false;
                }
    
                if (methodInfo == null)
                {
                    return false;
                }
    
                if (!methodInfo.IsPublic)
                {
                    return false;
                }
    
                if (methodInfo.IsDefined(typeof(AuditedAttribute), true))
                {
                    return true;
                }
    
                if (methodInfo.IsDefined(typeof(DisableAuditingAttribute), true))
                {
                    return false;
                }
    
                var classType = methodInfo.DeclaringType;
                if (classType != null)
                {
                    if (classType.GetTypeInfo().IsDefined(typeof(AuditedAttribute), true))
                    {
                        return true;
                    }
    
                    if (classType.GetTypeInfo().IsDefined(typeof(DisableAuditingAttribute), true))
                    {
                        return false;
                    }
    
                    if (_configuration.Selectors.Any(selector => selector.Predicate(classType)))
                    {
                        return true;
                    }
                }
    
                return defaultValue;
            }
    
            public AuditInfo CreateAuditInfo(Type type, MethodInfo method, object[] arguments)
            {
                return CreateAuditInfo(type, method, CreateArgumentsDictionary(method, arguments));
            }
    
            public AuditInfo CreateAuditInfo(Type type, MethodInfo method, IDictionary<string, object> arguments)
            {
                var auditInfo = new AuditInfo
                {
                    TenantId = AbpSession.TenantId,
                    UserId = AbpSession.UserId,
                    ImpersonatorUserId = AbpSession.ImpersonatorUserId,
                    ImpersonatorTenantId = AbpSession.ImpersonatorTenantId,
                    ServiceName = type != null
                        ? type.FullName
                        : "",
                    MethodName = method.Name,
                    Parameters = ConvertArgumentsToJson(arguments),
                    ExecutionTime = Clock.Now
                };
    
                try
                {
                    _auditInfoProvider.Fill(auditInfo);
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex.ToString(), ex);
                }
    
                return auditInfo;
            }
    
            public void Save(AuditInfo auditInfo)
            {
                using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
                {
                    AuditingStore.Save(auditInfo);
                    uow.Complete();
                }
            }
    
            public async Task SaveAsync(AuditInfo auditInfo)
            {
                using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
                {
                    await AuditingStore.SaveAsync(auditInfo);
                    await uow.CompleteAsync();
                }
            }
    
            private string ConvertArgumentsToJson(IDictionary<string, object> arguments)
            {
                try
                {
                    if (arguments.IsNullOrEmpty())
                    {
                        return "{}";
                    }
    
                    var dictionary = new Dictionary<string, object>();
    
                    foreach (var argument in arguments)
                    {
                        if (argument.Value != null && _configuration.IgnoredTypes.Any(t => t.IsInstanceOfType(argument.Value)))
                        {
                            dictionary[argument.Key] = null;
                        }
                        else
                        {
                            dictionary[argument.Key] = argument.Value;
                        }
                    }
    
                    return _auditSerializer.Serialize(dictionary);
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex.ToString(), ex);
                    return "{}";
                }
            }
    
            private static Dictionary<string, object> CreateArgumentsDictionary(MethodInfo method, object[] arguments)
            {
                var parameters = method.GetParameters();
                var dictionary = new Dictionary<string, object>();
    
                for (var i = 0; i < parameters.Length; i++)
                {
                    dictionary[parameters[i].Name] = arguments[i];
                }
    
                return dictionary;
            }
        }
    
    
  • User Avatar
    0
    velu created

    Hi,

    We have implemented this service in our project.

    Output result is

    Methode name : Authenticate Tenant Id : 1 userID : null

  • User Avatar
    0
    maliming created
    Support Team

    You can try methods that require user authentication.

  • User Avatar
    0
    velu created

    after implementation of this service, and authenticate the valid user at that time we got null userid.. (tokenauth/authente)

    how can we solve this

  • User Avatar
    0
    maliming created
    Support Team

    hi @velu

    Can you share your project or a simple project that reproduces the problem?