LocalFileEventLog.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // LocalFileEventLog.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2006 Novell, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Diagnostics;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design;
  33. using System.Collections;
  34. using System.Globalization;
  35. using System.IO;
  36. using System.Net;
  37. namespace System.Diagnostics
  38. {
  39. class LocalFileEventLogUtil
  40. {
  41. public const string DateFormat = "yyyyMMddHHmmssfff";
  42. static readonly string path;
  43. static LocalFileEventLogUtil ()
  44. {
  45. string env = Environment.GetEnvironmentVariable ("MONO_LOCAL_EVENTLOG_PATH");
  46. if (env != null)
  47. path = Path.GetFullPath (env);
  48. }
  49. public static bool IsEnabled {
  50. get { return path != null && Directory.Exists (path); }
  51. }
  52. public static string GetSourceDir (string source)
  53. {
  54. foreach (string log in GetLogDirectories ()) {
  55. string sd = Path.Combine (log, source);
  56. if (Directory.Exists (sd))
  57. return sd;
  58. }
  59. return null;
  60. }
  61. public static string GetLogDir (string logName)
  62. {
  63. return Path.Combine (Path.Combine (path, "logs"), logName);
  64. }
  65. public static string [] GetLogDirectories ()
  66. {
  67. return Directory.GetDirectories (Path.Combine (path, "logs"));
  68. }
  69. }
  70. class LocalFileEventLog : EventLogImpl
  71. {
  72. static readonly string [] empty_strings = new string [0];
  73. EventLog log;
  74. string source_path;
  75. public LocalFileEventLog (EventLog log)
  76. : base (log)
  77. {
  78. this.log = log;
  79. source_path = LocalFileEventLogUtil.GetSourceDir (log.Source);
  80. if (!Directory.Exists (source_path))
  81. throw new SystemException (String.Format ("INTERNAL ERROR: directory for {0} does not exist.", log.Source));
  82. }
  83. public override EventLogEntryCollection Entries {
  84. get {
  85. ArrayList list = new ArrayList ();
  86. int index = 0;
  87. foreach (string file in Directory.GetFiles (source_path, "*.log"))
  88. list.Add (LoadLogEntry (file, index++));
  89. return new EventLogEntryCollection ((EventLogEntry []) list.ToArray (typeof (EventLogEntry)));
  90. }
  91. }
  92. public override string LogDisplayName {
  93. get { return log.Log; }
  94. }
  95. EventLogEntry LoadLogEntry (string file, int index)
  96. {
  97. using (TextReader tr = File.OpenText (file)) {
  98. int id = int.Parse (tr.ReadLine ().Substring (9));
  99. EventLogEntryType type = (EventLogEntryType)
  100. Enum.Parse (typeof (EventLogEntryType), tr.ReadLine ().Substring (11));
  101. string category = tr.ReadLine ().Substring (10);
  102. int size = int.Parse (tr.ReadLine ().Substring (15));
  103. char [] buf = new char [size];
  104. tr.Read (buf, 0, size);
  105. string filename = Path.GetFileName (file).Substring (0, LocalFileEventLogUtil.DateFormat.Length);
  106. DateTime date = DateTime.ParseExact (filename, LocalFileEventLogUtil.DateFormat, CultureInfo.InvariantCulture);
  107. byte [] bin = Convert.FromBase64String (tr.ReadToEnd ());
  108. // FIXME: categoryNumber, index, userName, two dates
  109. return new EventLogEntry (category, 0, index,
  110. id, new string (buf), log.Source, "", log.MachineName,
  111. type, date, date, bin, empty_strings);
  112. }
  113. }
  114. public override void BeginInit ()
  115. {
  116. }
  117. public override void Clear ()
  118. {
  119. foreach (string file in Directory.GetFiles (source_path, "*.log"))
  120. File.Delete (file);
  121. }
  122. public override void Close ()
  123. {
  124. }
  125. public override void Dispose (bool disposing)
  126. {
  127. Close ();
  128. }
  129. public override void EndInit ()
  130. {
  131. }
  132. }
  133. // Creates a log repository at MONO_LOCAL_EVENTLOG_DIR, which consists of
  134. // -
  135. internal class LocalFileEventLogFactory : EventLogFactory
  136. {
  137. static readonly IPAddress local_ip = IPAddress.Parse ("127.0.0.1");
  138. public LocalFileEventLogFactory ()
  139. {
  140. }
  141. public override EventLogImpl Create (EventLog log)
  142. {
  143. if (!SourceExists (log.Source, log.MachineName))
  144. CreateEventSource (log.Source, log.Log, log.MachineName);
  145. return new LocalFileEventLog (log);
  146. }
  147. void VerifyMachine (string machineName)
  148. {
  149. if (machineName != ".") {
  150. IPHostEntry entry =
  151. #if NET_2_0
  152. Dns.GetHostEntry (machineName);
  153. #else
  154. Dns.Resolve (machineName);
  155. #endif
  156. if (Array.IndexOf (entry.AddressList, local_ip) < 0)
  157. throw new NotSupportedException (String.Format ("LocalFileEventLog does not support remote machine: {0}", machineName));
  158. }
  159. }
  160. public override void CreateEventSource (string source, string logName, string machineName)
  161. {
  162. VerifyMachine (machineName);
  163. string sourceDir = LocalFileEventLogUtil.GetSourceDir (source);
  164. if (sourceDir != null)
  165. throw new ArgumentException (String.Format ("Source '{0}' already exists on the local machine.", source));
  166. string logDir = LocalFileEventLogUtil.GetLogDir (logName);
  167. if (!Directory.Exists (logDir))
  168. Directory.CreateDirectory (logDir);
  169. Directory.CreateDirectory (Path.Combine (logDir, source));
  170. }
  171. public override void Delete (string logName, string machineName)
  172. {
  173. VerifyMachine (machineName);
  174. string logDir = LocalFileEventLogUtil.GetLogDir (logName);
  175. if (Directory.Exists (logDir))
  176. Directory.Delete (logDir);
  177. }
  178. public override void DeleteEventSource (string source, string machineName)
  179. {
  180. VerifyMachine (machineName);
  181. string sourceDir = LocalFileEventLogUtil.GetSourceDir (source);
  182. if (Directory.Exists (sourceDir))
  183. Directory.Delete (sourceDir);
  184. else
  185. throw new ArgumentException (String.Format ("Event source '{0}' does not exist on the local machine."), source);
  186. }
  187. public override bool Exists (string logName, string machineName)
  188. {
  189. VerifyMachine (machineName);
  190. return Directory.Exists (LocalFileEventLogUtil.GetLogDir (logName));
  191. }
  192. public override EventLog[] GetEventLogs (string machineName)
  193. {
  194. VerifyMachine (machineName);
  195. ArrayList al = new ArrayList ();
  196. foreach (string log in LocalFileEventLogUtil.GetLogDirectories ())
  197. al.Add (new EventLog (log));
  198. return (EventLog []) al.ToArray (typeof (EventLog));
  199. }
  200. public override string LogNameFromSourceName (string source, string machineName)
  201. {
  202. VerifyMachine (machineName);
  203. string sourceDir = LocalFileEventLogUtil.GetSourceDir (source);
  204. if (sourceDir == null)
  205. throw new ArgumentException (String.Format ("Event source '{0}' does not exist on the local machine."), source);
  206. return Directory.GetParent (sourceDir).Name;
  207. }
  208. public override bool SourceExists (string source, string machineName)
  209. {
  210. VerifyMachine (machineName);
  211. return LocalFileEventLogUtil.GetSourceDir (source) != null;
  212. }
  213. public override void WriteEntry (string source, string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
  214. {
  215. if (!SourceExists (source, "."))
  216. throw new ArgumentException (String.Format ("Event source '{0}' does not exist on the local machine."), source);
  217. string sourceDir = LocalFileEventLogUtil.GetSourceDir (source);
  218. string path = Path.Combine (sourceDir, DateTime.Now.ToString (LocalFileEventLogUtil.DateFormat) + ".log");
  219. try {
  220. using (TextWriter w = File.CreateText (path)) {
  221. w.WriteLine ("EventID: {0}", eventID);
  222. w.WriteLine ("EntryType: {0}", type);
  223. w.WriteLine ("Category: {0}", category);
  224. w.WriteLine ("MessageLength: {0}", message.Length);
  225. w.Write (message);
  226. if (rawData != null)
  227. w.Write (Convert.ToBase64String (rawData));
  228. }
  229. } catch (IOException) {
  230. File.Delete (path);
  231. }
  232. }
  233. }
  234. }