| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System.ComponentModel.DataAnnotations.Schema;
- using Abp.Auditing;
- using Abp.Domain.Entities;
- using Abp.Extensions;
- using Abp.Runtime.Validation;
- using Abp.UI;
- namespace VberZero.BaseSystem;
- /// <summary>
- ///
- /// </summary>
- [Table("Sys_AuditLogs")]
- public class AuditLog : Entity<long>, IMayHaveTenant
- {
- public static int MaxServiceNameLength = 256;
- public static int MaxMethodNameLength = 256;
- public static int MaxParametersLength = 1024;
- public static int MaxReturnValueLength = 1024;
- public static int MaxClientIpAddressLength = 64;
- public static int MaxClientNameLength = 128;
- public static int MaxBrowserInfoLength = 512;
- public static int MaxExceptionMessageLength = 1024;
- public static int MaxExceptionLength = 2000;
- public static int MaxCustomDataLength = 2000;
- public virtual int? TenantId { get; set; }
- public virtual long? UserId { get; set; }
- /// <summary>
- /// 服务(类/接口)名称
- /// </summary>
- public virtual string ServiceName { get; set; }
- /// <summary>
- /// 执行的方法名称
- /// </summary>
- public virtual string MethodName { get; set; }
- /// <summary>
- /// 服务(类/接口)名称 (Lang)
- /// </summary>
- public virtual string ServiceNameLang { get; set; }
- /// <summary>
- /// 执行的方法名称 (Lang)
- /// </summary>
- public virtual string MethodNameLang { get; set; }
- /// <summary>
- /// 调用参数
- /// </summary>
- public virtual string Parameters { get; set; }
- /// <summary>
- /// 返回值
- /// </summary>
- public virtual string ReturnValue { get; set; }
- /// <summary>
- /// 方法执行的开始时间。
- /// </summary>
- public virtual DateTime ExecutionTime { get; set; }
- /// <summary>
- /// 方法调用的总持续时间(以毫秒为单位)。
- /// </summary>
- public virtual int ExecutionDuration { get; set; }
- /// <summary>
- /// 客户端的 IP 地址。
- /// </summary>
- public virtual string ClientIpAddress { get; set; }
- /// <summary>
- /// 客户端的名称(通常是计算机名称)。
- /// </summary>
- public virtual string ClientName { get; set; }
- /// <summary>
- /// 如果在 Web 请求中调用此方法,则浏览器信息。
- /// </summary>
- public virtual string BrowserInfo { get; set; }
- /// <summary>
- /// 存储<see cref="Exception"/>的消息内容。
- /// </summary>
- public virtual string ExceptionMessage { get; set; }
- /// <summary>
- /// 异常对象,如果在方法执行期间发生异常。
- /// </summary>
- public virtual string Exception { get; set; }
- /// <summary>
- /// <see cref="AuditInfo.ImpersonatorUserId"/>.
- /// </summary>
- public virtual long? ImpersonatorUserId { get; set; }
- /// <summary>
- /// <see cref="AuditInfo.ImpersonatorTenantId"/>.
- /// </summary>
- public virtual int? ImpersonatorTenantId { get; set; }
- /// <summary>
- /// <see cref="AuditInfo.CustomData"/>.
- /// </summary>
- public virtual string CustomData { get; set; }
- public virtual int LogType { get; set; }
- /// <summary>
- /// 从给定的 <paramref name="auditInfo"/> 创建一个新的 CreateFromAuditInfo。
- /// </summary>
- /// <param name="auditInfo"></param>
- public static AuditLog CreateFromAuditInfo(AuditInfo auditInfo)
- {
- var exceptionMessage = GetAbpClearException(auditInfo.Exception);
- return new AuditLog
- {
- TenantId = auditInfo.TenantId,
- UserId = auditInfo.UserId,
- LogType = 0,
- ServiceName = auditInfo.ServiceName.TruncateWithPostfix(MaxServiceNameLength),
- MethodName = auditInfo.MethodName.TruncateWithPostfix(MaxMethodNameLength),
- Parameters = auditInfo.Parameters.TruncateWithPostfix(MaxParametersLength),
- ReturnValue = auditInfo.ReturnValue.TruncateWithPostfix(MaxReturnValueLength),
- 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),
- ExceptionMessage = auditInfo.Exception?.Message.TruncateWithPostfix(MaxExceptionMessageLength),
- 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.";
- }
- /// <summary>
- /// 使审计异常更加明确
- /// </summary>
- /// <param name="exception"></param>
- /// <returns></returns>
- public static string GetAbpClearException(Exception exception)
- {
- var clearMessage = "";
- switch (exception)
- {
- case null:
- return null;
- case AbpValidationException abpValidationException:
- clearMessage = "共有" + abpValidationException.ValidationErrors.Count + "条验证错误:";
- foreach (var validationResult in abpValidationException.ValidationErrors)
- {
- var memberNames = "";
- if (validationResult.MemberNames.Any())
- {
- memberNames = " (" + string.Join(", ", validationResult.MemberNames) + ")";
- }
- clearMessage += "\r\n" + validationResult.ErrorMessage + memberNames;
- }
- break;
- case UserFriendlyException userFriendlyException:
- clearMessage =
- $"错误代码:{userFriendlyException.Code}\r\n错误信息:{userFriendlyException.Details}";
- break;
- }
- return exception + (clearMessage.IsNullOrWhiteSpace() ? "" : "\r\n\r\n" + clearMessage);
- }
- }
|