Logger.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using NamedPipeWrapper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.IO.Pipes;
  7. using System.Linq;
  8. using System.Security.AccessControl;
  9. using System.Text;
  10. namespace InABox.Logging
  11. {
  12. public enum LogType
  13. {
  14. Information,
  15. Query,
  16. Update,
  17. Error
  18. }
  19. public static class Logger
  20. {
  21. private static object logfileLock = new object();
  22. private static NamedPipeServer<String> _pipe;
  23. private static String _name = "";
  24. public static String LogFolder = "";
  25. private static String TmpFolder = "";
  26. private static bool _started = false;
  27. private static bool _logtoconsole = false;
  28. private static LogType[] _logtypes = new LogType[] { LogType.Information, LogType.Query, LogType.Update, LogType.Error };
  29. public static void Start(String folder, bool logtoconsole = false, LogType[] logtypes = null)
  30. {
  31. _logtoconsole = logtoconsole;
  32. if (logtypes != null)
  33. _logtypes = logtypes;
  34. LogFolder = folder;
  35. TmpFolder = Path.Combine(LogFolder, "Temp");
  36. if (!Directory.Exists(TmpFolder))
  37. Directory.CreateDirectory(TmpFolder);
  38. String[] files = Directory.GetFiles(TmpFolder);
  39. foreach (var file in files)
  40. File.Delete(file);
  41. _name = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
  42. PipeSecurity sec = new System.IO.Pipes.PipeSecurity();
  43. sec.SetAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, AccessControlType.Allow));
  44. _pipe = new NamedPipeServer<String>(_name, sec);
  45. _pipe.ClientConnected += delegate (NamedPipeConnection<String, String> conn)
  46. {
  47. conn.PushMessage("Connected to " + _name);
  48. };
  49. _pipe.Start();
  50. _started = true;
  51. }
  52. public static void Send(LogType logtype, String user, String message)
  53. {
  54. if (!_started)
  55. return;
  56. if (!_logtypes.Any(x => x == logtype))
  57. return;
  58. String type = logtype == LogType.Information ? "INFO" : logtype == LogType.Query ? "READ" : logtype == LogType.Update ? "UPDATE" : "ERROR";
  59. String msg = String.Format("{0:HH:mm:ss.fff} {1} {2} {3}",
  60. DateTime.Now,
  61. type?.PadRight(6),
  62. user?.PadRight(12),
  63. message
  64. );
  65. if (_logtoconsole) //System.Environment.UserInteractive)
  66. Console.WriteLine(msg);
  67. if (_pipe != null)
  68. _pipe.PushMessage(msg);
  69. if (!String.IsNullOrEmpty(LogFolder))
  70. {
  71. if ((logtype == LogType.Information) || (logtype == LogType.Error) || (logtype == LogType.Query))
  72. UpdateLogFile(msg);
  73. else if (logtype == LogType.Update)
  74. UpdateJournal(message);
  75. }
  76. //if (logtype == LogType.Error)
  77. //{
  78. // using (EventLog eventLog = new EventLog("Application"))
  79. // {
  80. // eventLog.Source = "Application";
  81. // eventLog.WriteEntry(msg, EventLogEntryType.Error);
  82. // }
  83. //}
  84. }
  85. private static void UpdateLogFile(string msg)
  86. {
  87. String filename = Path.Combine(LogFolder, String.Format("{0:yyyy-MM-dd}.log", DateTime.Today));
  88. try
  89. {
  90. lock (logfileLock)
  91. {
  92. using (StreamWriter sw = new StreamWriter(filename, true, Encoding.UTF8, 65536))
  93. {
  94. sw.WriteLine(msg);
  95. sw.Close();
  96. }
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine("*** Failed to Update Log: " + e.Message);
  102. Console.WriteLine("*** Message: " + msg);
  103. }
  104. }
  105. private static void UpdateJournal(string sql)
  106. {
  107. String filename = Path.Combine(LogFolder, String.Format("{0:yyyy-MM-dd}.sql", DateTime.Today));
  108. try
  109. {
  110. lock (logfileLock)
  111. {
  112. using (StreamWriter sw = new StreamWriter(filename, true, Encoding.UTF8, 65536))
  113. {
  114. sw.WriteLine(sql);
  115. sw.Close();
  116. }
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. Console.WriteLine("*** Failed to Update SQL Journal: " + e.Message);
  122. Console.WriteLine("*** Message: " + sql);
  123. }
  124. }
  125. public static void Dump(String filename, byte[] bytes)
  126. {
  127. if (!_started)
  128. return;
  129. String file = Path.Combine(TmpFolder, String.Format("{0:yyyy-MM-dd HH-mm-ss-fff} {1}", DateTime.Now, filename));
  130. File.WriteAllBytes(file, bytes);
  131. }
  132. //public static void Save(String data, String tag, String suffix)
  133. //{
  134. // String filename = Path.Combine( TmpFolder, String.Format("{0:yyyy-MM-dd-HH-mm-ss-ffff} {1}.{2}", DateTime.Now,tag,suffix) );
  135. // try
  136. // {
  137. // File.WriteAllText(filename,data);
  138. // }
  139. // catch (Exception e)
  140. // {
  141. // Console.WriteLine("*** Failed to Save Dumpt File: " + e.Message);
  142. // Console.WriteLine("*** Message: " + data);
  143. // }
  144. //}
  145. }
  146. }