using System; using System.ComponentModel.DataAnnotations.Schema; using Abp.Auditing; using Abp.Domain.Entities; using Abp.Extensions; namespace IwbZero.Authorization.Base.SystemInfo { [Table("Sys_AuditLogs")] public class SysLog : Entity, IMustHaveTenant { /// /// Maximum length of property. /// public static int MaxServiceNameLength = 256; /// /// Maximum length of property. /// public static int MaxMethodNameLength = 256; /// /// Maximum length of property. /// public static int MaxParametersLength = 1024; /// /// Maximum length of property. /// public static int MaxClientIpAddressLength = 64; /// /// Maximum length of property. /// public static int MaxClientNameLength = 128; /// /// Maximum length of property. /// public static int MaxBrowserInfoLength = 512; /// /// Maximum length of property. /// public static int MaxExceptionLength = 2000; /// /// Maximum length of property. /// public static int MaxCustomDataLength = 2000; /// /// TenantId. /// [Index] public virtual int TenantId { get; set; } /// /// UserId. /// [Index] public virtual long? UserId { get; set; } /// /// UserId. /// public virtual string UserName { get; set; } /// /// Service (class/interface) name. /// public virtual string ServiceName { get; set; } /// /// Service (class/interface) name. /// public virtual string ServiceNameLang { get; set; } /// /// Service (class/interface) name. /// public virtual string MethodName { get; set; } /// /// Executed method name. /// public virtual string MethodNameLang { get; set; } /// /// Calling parameters. /// public virtual string Parameters { get; set; } /// /// Start time of the method execution. /// public virtual DateTime ExecutionTime { get; set; } /// /// Total duration of the method call as milliseconds. /// public virtual int ExecutionDuration { get; set; } /// /// IP address of the client. /// public virtual string ClientIpAddress { get; set; } /// /// Name (generally computer name) of the client. /// public virtual string ClientName { get; set; } /// /// Browser information if this method is called in a web request. /// public virtual string BrowserInfo { get; set; } /// /// Exception object, if an exception occured during execution of the method. /// public virtual string Exception { get; set; } /// /// . /// public virtual long? ImpersonatorUserId { get; set; } /// /// . /// public virtual int? ImpersonatorTenantId { get; set; } public virtual string CustomData { get; set; } public virtual int LogType { get; set; } public virtual SysLog CreateFromAuditInfo(AuditInfo auditInfo, string userName) { var exceptionMessage = auditInfo.Exception?.ToString(); return new SysLog { TenantId = auditInfo.TenantId ?? 1, UserId = auditInfo.UserId, UserName = userName, //LogType = logType, 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) }; } public override string ToString() { return $"AUDIT LOG: {ServiceName}.{MethodName} is executed by user {UserId} in {ExecutionDuration} ms from {ClientIpAddress} IP address."; } } }