FileSystemStorage.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/Log/EventLogStorage/FileSystemStorage.cs $
  4. *
  5. * 3 08-03-19 9:05 roger.månsson
  6. * Exchange illegal file name characters with '_' in event file name, and
  7. * also exchange '\' characters with '_'.
  8. *
  9. * 2 08-02-13 9:24 Mattias.larsson
  10. * FxCop fixes.
  11. */
  12. #endregion
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Xml;
  16. using System.IO;
  17. using Wayne.Lib.IO;
  18. namespace Wayne.Lib.Log
  19. {
  20. /// <summary>
  21. /// The FileSystemStorage event storage class stores the events in the file system which
  22. /// enables them to survive a system restart.
  23. /// </summary>
  24. internal class FileSystemStorage : ISubscriptionStorage
  25. {
  26. #region Fields
  27. private object dbLock = new object();
  28. private string basePath;
  29. #endregion
  30. #region Construction
  31. public FileSystemStorage()
  32. {
  33. basePath = Paths.Combine("EventStorage", System.AppDomain.CurrentDomain.FriendlyName);
  34. FileSupport.EnsureDirectoryExists(basePath);
  35. }
  36. #endregion
  37. #region Methods
  38. public void Remove(string eventSubscriberId, EventLogEntry eventLogEntry)
  39. {
  40. GetAndCreateSubscriberDir(eventSubscriberId);
  41. lock (dbLock)
  42. {
  43. string fileName = Path.Combine(basePath, Path.Combine(eventSubscriberId, CreateFileName(eventLogEntry)));
  44. if (File.Exists(fileName))
  45. File.Delete(fileName);
  46. }
  47. }
  48. public EventLogEntry[] GetStoredEvents(string eventSubscriberId)
  49. {
  50. GetAndCreateSubscriberDir(eventSubscriberId);
  51. lock (dbLock)
  52. {
  53. List<EventLogEntry> eventLogEntryList = new List<EventLogEntry>();
  54. string[] filePaths = Directory.GetFiles(Path.Combine(basePath, eventSubscriberId));
  55. XmlDocument xmlDoc = new XmlDocument();
  56. foreach (string filePath in filePaths)
  57. {
  58. FileSupport.LoadXml(xmlDoc, filePath);
  59. EventLogEntry entry = EventLogEntry.Deserialize(xmlDoc["LogEntry", EventLogXml.Ns]);
  60. if (entry != null)
  61. eventLogEntryList.Add(entry);
  62. }
  63. return eventLogEntryList.ToArray();
  64. }
  65. }
  66. public void Add(string eventSubscriberId, EventLogEntry eventLogEntry)
  67. {
  68. GetAndCreateSubscriberDir(eventSubscriberId);
  69. lock (dbLock)
  70. {
  71. string subscriberPath = GetAndCreateSubscriberDir(eventSubscriberId);
  72. string eventFileName = CreateFileName(eventLogEntry);
  73. XmlWriterSettings settings = new XmlWriterSettings();
  74. settings.Encoding = Encoding.UTF8;
  75. settings.Indent = true;
  76. settings.NewLineHandling = NewLineHandling.Entitize;
  77. using (XmlWriter xmlWriter = XmlWriter.Create(Path.Combine(subscriberPath, eventFileName), settings))
  78. {
  79. eventLogEntry.WriteXml(xmlWriter, "");
  80. }
  81. }
  82. }
  83. private string GetAndCreateSubscriberDir(string eventSubscriberId)
  84. {
  85. string subscriberPath = Path.Combine(basePath, eventSubscriberId);
  86. FileSupport.EnsureDirectoryExists(subscriberPath);
  87. return subscriberPath;
  88. }
  89. private static string CreateFileName(EventLogEntry eventLogEntry)
  90. {
  91. string eventFileName = eventLogEntry.EntityCategory.GetName(EntityLogKind.Entity, false) + eventLogEntry.GetDateTimeString("yyyyMMddHHmmss") + ".xml";
  92. foreach (char invalidChar in Path.GetInvalidPathChars())
  93. {
  94. eventFileName = eventFileName.Replace(invalidChar, '_');
  95. }
  96. eventFileName = eventFileName.Replace('\\', '_');
  97. return eventFileName;
  98. }
  99. #endregion
  100. }
  101. }