AuditLogInfo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Microsoft.CodeAnalysis.CSharp.Syntax;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Text.Json.Serialization;
  9. namespace Edge.Core.UniversalApi.Auditing
  10. {
  11. public class AuditLogInfo
  12. {
  13. private static JsonSerializerOptions jsonSerializerOptions;
  14. static AuditLogInfo()
  15. {
  16. jsonSerializerOptions = new JsonSerializerOptions()
  17. {
  18. WriteIndented = true,
  19. PropertyNameCaseInsensitive = true,
  20. };
  21. jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
  22. }
  23. /// <summary>
  24. /// UniversalApi must targeting on a Processor's Endpoint, that is DeviceHanlder or App.
  25. /// </summary>
  26. public string DeviceHandlerOrAppName { get; set; }
  27. /// <summary>
  28. /// The time when this audit log object has been created.
  29. /// </summary>
  30. public DateTime ExecutionTime { get; set; }
  31. /// <summary>
  32. /// Total execution duration of the request, in milliseconds. This can be used to observe the performance of the application.
  33. /// </summary>
  34. public long ExecutionDuration { get; set; }
  35. /// <summary>
  36. /// Id of the current client, for http request, it would be remote client Ip+port.
  37. /// for mqtt request, it would be mqtt connect user name.
  38. /// </summary>
  39. public string ClientIdentity { get; set; }
  40. /// <summary>
  41. /// An audit log object may contain zero or more exception. In this way, you can get a report of the failed requests.
  42. /// </summary>
  43. public List<Exception> Exceptions { get; }
  44. public List<AuditLogActionInfo> Actions { get; }
  45. public AuditLogInfo()
  46. {
  47. Actions = new List<AuditLogActionInfo>();
  48. Exceptions = new List<Exception>();
  49. }
  50. private Stopwatch stopwatch = null;
  51. public void Prepare()
  52. {
  53. this.stopwatch = Stopwatch.StartNew();
  54. }
  55. public void CommitWithExeResult(object executeResult)
  56. {
  57. if (this.Actions.FirstOrDefault() != null && executeResult != null)
  58. {
  59. var jsResult = JsonSerializer.Serialize(executeResult, jsonSerializerOptions);
  60. this.Actions.First().OutputResults = jsResult;
  61. }
  62. this.ExecutionDuration = this.stopwatch.ElapsedMilliseconds;
  63. this.stopwatch.Stop();
  64. }
  65. public void CommitWithExceptions(IEnumerable<Exception> exceptions)
  66. {
  67. this.ExecutionDuration = this.stopwatch.ElapsedMilliseconds;
  68. this.stopwatch.Stop();
  69. this.Exceptions.AddRange(exceptions);
  70. }
  71. public override string ToString()
  72. {
  73. var sb = new StringBuilder();
  74. sb.AppendLine($"AUDIT LOG: [{(DeviceHandlerOrAppName ?? "---")} - {(Actions.FirstOrDefault()?.ApiName ?? "-------").PadRight(7)}]");
  75. sb.AppendLine($"- ClientIdentity : {ClientIdentity}");
  76. sb.AppendLine($"- ExecutionTime : {ExecutionTime.ToString("yyyy-MM-dd HH:mm:ss fff")}");
  77. sb.AppendLine($"- ExecutionDuration: {ExecutionDuration}");
  78. if (Actions != null)
  79. {
  80. sb.AppendLine("- Actions:");
  81. foreach (var action in Actions)
  82. {
  83. sb.AppendLine($" - {action.ApiName}");
  84. sb.AppendLine($" Input : {action.InputParameters}");
  85. sb.AppendLine($" Output: {action.OutputResults}");
  86. }
  87. }
  88. if (Exceptions.Any())
  89. {
  90. sb.AppendLine("- Exceptions:");
  91. foreach (var exception in Exceptions)
  92. {
  93. sb.AppendLine($" - {exception.Message}");
  94. sb.AppendLine($" {exception}");
  95. }
  96. }
  97. return sb.ToString();
  98. }
  99. }
  100. public class AuditLogActionInfo
  101. {
  102. public string ApiName { get; set; }
  103. public string InputParameters { get; set; }
  104. public string OutputResults { get; set; }
  105. }
  106. }