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;
///
///
///
[Table("Sys_AuditLogs")]
public class AuditLog : Entity, 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; }
///
/// 服务(类/接口)名称
///
public virtual string ServiceName { get; set; }
///
/// 执行的方法名称
///
public virtual string MethodName { get; set; }
///
/// 服务(类/接口)名称 (Lang)
///
public virtual string ServiceNameLang { get; set; }
///
/// 执行的方法名称 (Lang)
///
public virtual string MethodNameLang { get; set; }
///
/// 调用参数
///
public virtual string Parameters { get; set; }
///
/// 返回值
///
public virtual string ReturnValue { get; set; }
///
/// 方法执行的开始时间。
///
public virtual DateTime ExecutionTime { get; set; }
///
/// 方法调用的总持续时间(以毫秒为单位)。
///
public virtual int ExecutionDuration { get; set; }
///
/// 客户端的 IP 地址。
///
public virtual string ClientIpAddress { get; set; }
///
/// 客户端的名称(通常是计算机名称)。
///
public virtual string ClientName { get; set; }
///
/// 如果在 Web 请求中调用此方法,则浏览器信息。
///
public virtual string BrowserInfo { get; set; }
///
/// 存储的消息内容。
///
public virtual string ExceptionMessage { get; set; }
///
/// 异常对象,如果在方法执行期间发生异常。
///
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; }
///
/// 从给定的 创建一个新的 CreateFromAuditInfo。
///
///
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.";
}
///
/// 使审计异常更加明确
///
///
///
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);
}
}