SysLog.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Abp.Auditing;
  4. using Abp.Domain.Entities;
  5. using Abp.Extensions;
  6. namespace IwbZero.Authorization.Base.SystemInfo
  7. {
  8. [Table("Sys_AuditLogs")]
  9. public class SysLog : Entity<long>, IMustHaveTenant
  10. {
  11. /// <summary>
  12. /// Maximum length of <see cref="ServiceName"/> property.
  13. /// </summary>
  14. public static int MaxServiceNameLength = 256;
  15. /// <summary>
  16. /// Maximum length of <see cref="MethodName"/> property.
  17. /// </summary>
  18. public static int MaxMethodNameLength = 256;
  19. /// <summary>
  20. /// Maximum length of <see cref="Parameters"/> property.
  21. /// </summary>
  22. public static int MaxParametersLength = 1024;
  23. /// <summary>
  24. /// Maximum length of <see cref="ClientIpAddress"/> property.
  25. /// </summary>
  26. public static int MaxClientIpAddressLength = 64;
  27. /// <summary>
  28. /// Maximum length of <see cref="ClientName"/> property.
  29. /// </summary>
  30. public static int MaxClientNameLength = 128;
  31. /// <summary>
  32. /// Maximum length of <see cref="BrowserInfo"/> property.
  33. /// </summary>
  34. public static int MaxBrowserInfoLength = 512;
  35. /// <summary>
  36. /// Maximum length of <see cref="Exception"/> property.
  37. /// </summary>
  38. public static int MaxExceptionLength = 2000;
  39. /// <summary>
  40. /// Maximum length of <see cref="CustomData"/> property.
  41. /// </summary>
  42. public static int MaxCustomDataLength = 2000;
  43. /// <summary>
  44. /// TenantId.
  45. /// </summary>
  46. [Index]
  47. public virtual int TenantId { get; set; }
  48. /// <summary>
  49. /// UserId.
  50. /// </summary>
  51. [Index]
  52. public virtual long? UserId { get; set; }
  53. /// <summary>
  54. /// UserId.
  55. /// </summary>
  56. public virtual string UserName { get; set; }
  57. /// <summary>
  58. /// Service (class/interface) name.
  59. /// </summary>
  60. public virtual string ServiceName { get; set; }
  61. /// <summary>
  62. /// Service (class/interface) name.
  63. /// </summary>
  64. public virtual string ServiceNameLang { get; set; }
  65. /// <summary>
  66. /// Service (class/interface) name.
  67. /// </summary>
  68. public virtual string MethodName { get; set; }
  69. /// <summary>
  70. /// Executed method name.
  71. /// </summary>
  72. public virtual string MethodNameLang { get; set; }
  73. /// <summary>
  74. /// Calling parameters.
  75. /// </summary>
  76. public virtual string Parameters { get; set; }
  77. /// <summary>
  78. /// Start time of the method execution.
  79. /// </summary>
  80. public virtual DateTime ExecutionTime { get; set; }
  81. /// <summary>
  82. /// Total duration of the method call as milliseconds.
  83. /// </summary>
  84. public virtual int ExecutionDuration { get; set; }
  85. /// <summary>
  86. /// IP address of the client.
  87. /// </summary>
  88. public virtual string ClientIpAddress { get; set; }
  89. /// <summary>
  90. /// Name (generally computer name) of the client.
  91. /// </summary>
  92. public virtual string ClientName { get; set; }
  93. /// <summary>
  94. /// Browser information if this method is called in a web request.
  95. /// </summary>
  96. public virtual string BrowserInfo { get; set; }
  97. /// <summary>
  98. /// Exception object, if an exception occured during execution of the method.
  99. /// </summary>
  100. public virtual string Exception { get; set; }
  101. /// <summary>
  102. /// <see cref="AuditInfo.ImpersonatorUserId"/>.
  103. /// </summary>
  104. public virtual long? ImpersonatorUserId { get; set; }
  105. /// <summary>
  106. /// <see cref="AuditInfo.ImpersonatorTenantId"/>.
  107. /// </summary>
  108. public virtual int? ImpersonatorTenantId { get; set; }
  109. public virtual string CustomData { get; set; }
  110. public virtual int LogType { get; set; }
  111. public virtual SysLog CreateFromAuditInfo(AuditInfo auditInfo, string userName)
  112. {
  113. var exceptionMessage = auditInfo.Exception?.ToString();
  114. return new SysLog
  115. {
  116. TenantId = auditInfo.TenantId ?? 1,
  117. UserId = auditInfo.UserId,
  118. UserName = userName,
  119. //LogType = logType,
  120. ServiceName = auditInfo.ServiceName.TruncateWithPostfix(MaxServiceNameLength),
  121. MethodName = auditInfo.MethodName.TruncateWithPostfix(MaxMethodNameLength),
  122. Parameters = auditInfo.Parameters.TruncateWithPostfix(MaxParametersLength),
  123. ExecutionTime = auditInfo.ExecutionTime,
  124. ExecutionDuration = auditInfo.ExecutionDuration,
  125. ClientIpAddress = auditInfo.ClientIpAddress.TruncateWithPostfix(MaxClientIpAddressLength),
  126. ClientName = auditInfo.ClientName.TruncateWithPostfix(MaxClientNameLength),
  127. BrowserInfo = auditInfo.BrowserInfo.TruncateWithPostfix(MaxBrowserInfoLength),
  128. Exception = exceptionMessage.TruncateWithPostfix(MaxExceptionLength),
  129. ImpersonatorUserId = auditInfo.ImpersonatorUserId,
  130. ImpersonatorTenantId = auditInfo.ImpersonatorTenantId,
  131. CustomData = auditInfo.CustomData.TruncateWithPostfix(MaxCustomDataLength)
  132. };
  133. }
  134. public override string ToString()
  135. {
  136. return
  137. $"AUDIT LOG: {ServiceName}.{MethodName} is executed by user {UserId} in {ExecutionDuration} ms from {ClientIpAddress} IP address.";
  138. }
  139. }
  140. }