You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Text;
  6. namespace Newtonsoft.Json.Serialization
  7. {
  8. /// <summary>
  9. /// Represents a trace writer that writes to memory. When the trace message limit is
  10. /// reached then old trace messages will be removed as new messages are added.
  11. /// </summary>
  12. internal class MemoryTraceWriter : ITraceWriter
  13. {
  14. private readonly Queue<string> _traceMessages;
  15. private readonly object _lock;
  16. /// <summary>
  17. /// Gets the <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
  18. /// For example a filter level of <see cref="TraceLevel.Info"/> will exclude <see cref="TraceLevel.Verbose"/> messages and include <see cref="TraceLevel.Info"/>,
  19. /// <see cref="TraceLevel.Warning"/> and <see cref="TraceLevel.Error"/> messages.
  20. /// </summary>
  21. /// <value>
  22. /// The <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
  23. /// </value>
  24. public TraceLevel LevelFilter { get; set; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="MemoryTraceWriter"/> class.
  27. /// </summary>
  28. public MemoryTraceWriter()
  29. {
  30. LevelFilter = TraceLevel.Verbose;
  31. _traceMessages = new Queue<string>();
  32. _lock = new object();
  33. }
  34. /// <summary>
  35. /// Writes the specified trace level, message and optional exception.
  36. /// </summary>
  37. /// <param name="level">The <see cref="TraceLevel"/> at which to write this trace.</param>
  38. /// <param name="message">The trace message.</param>
  39. /// <param name="ex">The trace exception. This parameter is optional.</param>
  40. public void Trace(TraceLevel level, string message, Exception ex)
  41. {
  42. StringBuilder sb = new StringBuilder();
  43. sb.Append(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff", CultureInfo.InvariantCulture));
  44. sb.Append(" ");
  45. sb.Append(level.ToString("g"));
  46. sb.Append(" ");
  47. sb.Append(message);
  48. string s = sb.ToString();
  49. lock (_lock)
  50. {
  51. if (_traceMessages.Count >= 1000)
  52. {
  53. _traceMessages.Dequeue();
  54. }
  55. _traceMessages.Enqueue(s);
  56. }
  57. }
  58. /// <summary>
  59. /// Returns an enumeration of the most recent trace messages.
  60. /// </summary>
  61. /// <returns>An enumeration of the most recent trace messages.</returns>
  62. public IEnumerable<string> GetTraceMessages()
  63. {
  64. return _traceMessages;
  65. }
  66. /// <summary>
  67. /// Returns a <see cref="String"/> of the most recent trace messages.
  68. /// </summary>
  69. /// <returns>
  70. /// A <see cref="String"/> of the most recent trace messages.
  71. /// </returns>
  72. public override string ToString()
  73. {
  74. lock (_lock)
  75. {
  76. StringBuilder sb = new StringBuilder();
  77. foreach (string traceMessage in _traceMessages)
  78. {
  79. if (sb.Length > 0)
  80. {
  81. sb.AppendLine();
  82. }
  83. sb.Append(traceMessage);
  84. }
  85. return sb.ToString();
  86. }
  87. }
  88. }
  89. }