Logger.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4. using System.IO;
  5. using System.Threading;
  6. using Wayne.Lib;
  7. using Wayne.Lib.Log;
  8. namespace Wayne.ForecourtControl.Fusion
  9. {
  10. //public delegate void EventHandler<ConnectionChangedEventArgs>(Object sender, EventArgs e);
  11. public static class Trace
  12. {
  13. public static DebugLogger debugLogger = null;
  14. public static SSTrace sstrace;
  15. public static void Add(IIdentifiableEntity logclass)
  16. {
  17. try
  18. {
  19. string sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "Trace", "FControlFUSION");
  20. if (sValue != "")
  21. {
  22. sstrace = new SSTrace(string.Format("{0}{1}", ConfigurationParams.getSINPPath("trace\\"), sValue));
  23. }
  24. else if (debugLogger == null)
  25. debugLogger = new DebugLogger(logclass, true);
  26. }
  27. catch (Exception ex)
  28. {
  29. }
  30. }
  31. public static void WriteLine(string msg)
  32. {
  33. try
  34. {
  35. #if (_NUCLEUS_COMPATIBLE || WindowsCE)
  36. if (debugLogger != null && debugLogger.IsActive())
  37. {
  38. //string methodName = System.Reflection.MethodBase.GetMethodFromHandle().Name;
  39. debugLogger.Add(msg);
  40. }
  41. if (sstrace != null)
  42. sstrace.WriteLine(msg);
  43. #else
  44. System.Diagnostics.Trace.WriteLine(msg);
  45. if (debugLogger != null && debugLogger.IsActive())
  46. {
  47. //string methodName = System.Reflection.MethodBase.GetMethodFromHandle().Name;
  48. debugLogger.Add(msg);
  49. }
  50. #endif
  51. }
  52. catch (Exception ex)
  53. {
  54. }
  55. }
  56. public static void WriteLineIf(bool condition, string msg)
  57. {
  58. try
  59. {
  60. #if (_NUCLEUS_COMPATIBLE || WindowsCE)
  61. if (debugLogger != null && debugLogger.IsActive() && (condition || debugLogger.GetDebugLevel() == DebugLogLevel.Detailed))
  62. {
  63. //string methodName = System.Reflection.MethodBase.GetMethodFromHandle().Name;
  64. debugLogger.Add(msg);
  65. }
  66. if (sstrace != null && condition)
  67. sstrace.WriteLine(msg);
  68. #else
  69. System.Diagnostics.Trace.WriteLineIf(condition, msg);
  70. #endif
  71. }
  72. catch (Exception ex)
  73. {
  74. }
  75. }
  76. public static bool CheckTraceLevel(int iLevel)
  77. {
  78. int TraceLevel = 0;
  79. try
  80. {
  81. string sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "Trace", "TraceLevel");
  82. if (sValue != "")
  83. {
  84. TraceLevel = Convert.ToInt32(sValue);
  85. }
  86. else
  87. {
  88. sValue = IniFile.IniReadValue(ConfigurationParams.getSINPPath("ini\\") + @"Common.ini", "Trace", "TraceLevel");
  89. if (sValue != "")
  90. {
  91. TraceLevel = Convert.ToInt32(sValue);
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. TraceLevel = 0;
  98. }
  99. if (iLevel <= TraceLevel)
  100. return true;
  101. return false;
  102. }
  103. }
  104. public class SSTrace
  105. {
  106. private FileStream fs;
  107. private StreamWriter sw;
  108. private string strFileTrace;
  109. public string FileTrace
  110. {
  111. get { return strFileTrace; }
  112. set { strFileTrace = value; }
  113. }
  114. private string strFullFileTrace;
  115. public string FullFileTrace
  116. {
  117. get { return strFullFileTrace; }
  118. set { strFullFileTrace = value; }
  119. }
  120. private int MaxDaysTrace = 0;
  121. private int NewDay = -1;
  122. private int fileSize = 24;
  123. public SSTrace(string sFileTrace)
  124. {
  125. FileTrace = sFileTrace;
  126. NewDay = DateTime.Now.Day;
  127. try
  128. {
  129. string sValue;
  130. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "Trace", "Size");
  131. if (sValue != "")
  132. {
  133. fileSize = Convert.ToInt32(sValue);
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. fileSize = 24;
  139. }
  140. }
  141. public void PreparaStream()
  142. {
  143. if (fs != null)
  144. {
  145. fs.Close();
  146. }
  147. fs = new FileStream(FullFileTrace, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  148. sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(1252));
  149. sw.AutoFlush = true;
  150. //Writer = sw;
  151. }
  152. public void ChiudiStream()
  153. {
  154. if (fs != null)
  155. {
  156. fs.Close();
  157. }
  158. }
  159. public void WriteLine(string strTrace)
  160. {
  161. if (FileTrace.Length == 0)
  162. {
  163. return;
  164. }
  165. lock(this);
  166. try
  167. {
  168. string strLineTrace;
  169. strLineTrace = string.Format("{0}: {1}", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ffff"), strTrace);
  170. bool bNuovoFile = false;
  171. if (FullFileTrace == null)
  172. {
  173. DateTime now = DateTime.Now;
  174. FullFileTrace = string.Format("{0}\\{1:00}-{2:00}-{3}", Path.GetDirectoryName(FileTrace), now.Day, now.Month, Path.GetFileName(FileTrace));
  175. }
  176. FileInfo fi = new FileInfo(FullFileTrace);
  177. if (fi.Exists && fi.Length >= System.Math.Pow(2, fileSize))
  178. {
  179. fi.MoveTo(FullFileTrace + DateTime.Now.ToString("HH:mm:ss") + ".bck");
  180. bNuovoFile = true;
  181. }
  182. if (FullFileTrace == null || !File.Exists(FullFileTrace) || bNuovoFile)
  183. {
  184. bNuovoFile = true;
  185. DateTime now = DateTime.Now;
  186. FullFileTrace = string.Format("{0}\\{1:00}-{2:00}-{3}", Path.GetDirectoryName(FileTrace), now.Day, now.Month, Path.GetFileName(FileTrace));
  187. }
  188. PreparaStream();
  189. sw.WriteLine(strLineTrace);
  190. sw.Flush();
  191. ChiudiStream();
  192. CheckOldFiles();
  193. }
  194. catch (Exception ex)
  195. {
  196. ChiudiStream();
  197. }
  198. finally
  199. {
  200. }
  201. }
  202. public void CheckOldFiles()
  203. {
  204. if (NewDay != DateTime.Now.Day)
  205. {
  206. NewDay = DateTime.Now.Day;
  207. Thread th = new Thread(new ThreadStart(this.RemoveOldFiles));
  208. th.Start();
  209. }
  210. }
  211. private void RemoveOldFiles()
  212. {
  213. try
  214. {
  215. string sValue;
  216. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "Trace", "MaxDaysTrace");
  217. if (sValue != "")
  218. {
  219. MaxDaysTrace = Convert.ToInt32(sValue);
  220. }
  221. else
  222. {
  223. sValue = IniFile.IniReadValue(ConfigurationParams.getSINPPath("ini\\") + @"Common.ini", "Trace", "MaxDaysTrace");
  224. if (sValue != "")
  225. {
  226. MaxDaysTrace = Convert.ToInt32(sValue);
  227. }
  228. }
  229. }
  230. catch (Exception ex)
  231. {
  232. MaxDaysTrace = 5;
  233. }
  234. if (MaxDaysTrace < 0)
  235. {
  236. return;
  237. }
  238. TimeSpan tsDays;
  239. try
  240. {
  241. string strFilesTrace = Path.GetFileName(FileTrace) + "*.bck";
  242. WriteLine(string.Format("check deleting files: {0}, MaxDaysTrace={1}", strFilesTrace, MaxDaysTrace));
  243. DirectoryInfo di = new DirectoryInfo(ConfigurationParams.tracePath);
  244. // Print out the names of the files in the current directory.
  245. foreach (FileInfo fi in di.GetFiles(strFilesTrace))
  246. {
  247. tsDays = DateTime.Now - fi.CreationTime;
  248. if (tsDays.Days >= MaxDaysTrace)
  249. {
  250. WriteLine(string.Format("tsDays.Days={0}, Deleting file: {1}", tsDays.Days, fi.Name));
  251. fi.Delete();
  252. WriteLine(string.Format("Deleted file: {0}", fi.Name));
  253. }
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. }
  259. }
  260. }
  261. public static class IniFile
  262. {
  263. [DllImport("kernel32")]
  264. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  265. [DllImport("kernel32")]
  266. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  267. [DllImport("kernel32")]
  268. private static extern int GetPrivateProfileSection(string section, IntPtr retVal, int size, string filePath);
  269. public static string IniReadValue(string Path, string Section, string Key)
  270. {
  271. StringBuilder temp = new StringBuilder(255);
  272. int i = GetPrivateProfileString(Section, Key, "", temp, 255, Path);
  273. return temp.ToString();
  274. }
  275. public static void IniWriteValue(string Path, string Section, string Key, string Value)
  276. {
  277. WritePrivateProfileString(Section, Key, Value, Path);
  278. }
  279. }
  280. }