25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Log.cs 11 KiB

7 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Diagnostics;
  3. namespace Log
  4. {
  5. #region Log
  6. namespace Log
  7. {
  8. public class ParameterCountMismatchArgumentCountException : Exception
  9. {
  10. public ParameterCountMismatchArgumentCountException()
  11. : base() { }
  12. public ParameterCountMismatchArgumentCountException(string message)
  13. : base(message)
  14. { }
  15. }
  16. public class Log : Microsoft.VisualBasic.Logging.Log, IDisposable
  17. {
  18. public Log()
  19. : base()
  20. {
  21. if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["LogPath"]))
  22. {
  23. string LogPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
  24. string LogName = System.Reflection.Assembly.GetCallingAssembly().GetName().Name;
  25. DefaultFileLogWriter.CustomLocation = System.IO.Path.GetDirectoryName(LogPath);
  26. DefaultFileLogWriter.BaseFileName = LogName;
  27. DefaultFileLogWriter.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom;
  28. DefaultFileLogWriter.CustomLocation = LogPath;
  29. }
  30. else if (System.Reflection.Assembly.GetEntryAssembly() != null)
  31. {
  32. DefaultFileLogWriter.Location = Microsoft.VisualBasic.Logging.LogFileLocation.ExecutableDirectory;
  33. }
  34. DefaultFileLogWriter.Append = true;
  35. DefaultFileLogWriter.AutoFlush = true;
  36. DefaultFileLogWriter.Delimiter = ";";
  37. DefaultFileLogWriter.MaxFileSize = 2621440000; //2500 MB
  38. DefaultFileLogWriter.ReserveDiskSpace = 1048576000; //1000 MB
  39. DefaultFileLogWriter.LogFileCreationSchedule = Microsoft.VisualBasic.Logging.LogFileCreationScheduleOption.Daily;
  40. if (System.Configuration.ConfigurationManager.AppSettings["Debug"] == "true")
  41. TraceSource.Switch.Level = SourceLevels.All;
  42. else
  43. TraceSource.Switch.Level = SourceLevels.Information;
  44. WriteEntry("Starting log...");
  45. WriteEntry("Log instance created");
  46. }
  47. public Log(string name)
  48. : this()
  49. {
  50. DefaultFileLogWriter.BaseFileName = name;
  51. }
  52. public Log(string name, string logPath, SourceLevels level)
  53. : this()
  54. {
  55. DefaultFileLogWriter.CustomLocation = System.IO.Path.GetDirectoryName(logPath);
  56. DefaultFileLogWriter.BaseFileName = name;
  57. DefaultFileLogWriter.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom;
  58. DefaultFileLogWriter.CustomLocation = logPath;
  59. DefaultFileLogWriter.Append = true;
  60. DefaultFileLogWriter.AutoFlush = true;
  61. DefaultFileLogWriter.Delimiter = ";";
  62. DefaultFileLogWriter.MaxFileSize = 2621440000; //2500 MB
  63. DefaultFileLogWriter.ReserveDiskSpace = 1048576000; //1000 MB
  64. DefaultFileLogWriter.LogFileCreationSchedule = Microsoft.VisualBasic.Logging.LogFileCreationScheduleOption.Daily;
  65. TraceSource.Switch.Level = level;
  66. WriteEntry("Starting log...");
  67. WriteEntry("Log instance created");
  68. }
  69. public void Dispose()
  70. {
  71. WriteEntry("Log Class gets disposed.");
  72. WriteEntry("Closing log...");
  73. DefaultFileLogWriter.Close();
  74. }
  75. public void WriteFunctionEntry()
  76. {
  77. string message = string.Empty;
  78. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  79. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  80. message = string.Format("{0} : {1}(", callingClass, callingMethod);
  81. System.Reflection.ParameterInfo[] parameterInfos = new StackFrame(1, true).GetMethod().GetParameters();
  82. foreach (System.Reflection.ParameterInfo parameterInfo in parameterInfos)
  83. {
  84. message += parameterInfo.ParameterType.Name + " " + parameterInfo.Name + ", ";
  85. }
  86. //remove the "," at the end of the line
  87. if (message.EndsWith(", ")) { message = message.Substring(0, message.Length - 2); }
  88. message += ") : entering...";
  89. WriteEntry(message, TraceEventType.Verbose);
  90. }
  91. public void WriteFunctionEntry(params object[] args)
  92. {
  93. string message = string.Empty;
  94. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  95. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  96. message = string.Format("{0} : {1}(", callingClass, callingMethod);
  97. System.Reflection.ParameterInfo[] parameterInfos = new StackFrame(1, true).GetMethod().GetParameters();
  98. if (parameterInfos.Length != args.Length)
  99. {
  100. throw new ParameterCountMismatchArgumentCountException();
  101. }
  102. int parameterIterator = 0;
  103. foreach (System.Reflection.ParameterInfo parameterInfo in parameterInfos)
  104. {
  105. message += parameterInfo.ParameterType.Name + " " + parameterInfo.Name;
  106. message += string.Format("={0}, ",
  107. args[parameterIterator] != null ?
  108. args[parameterIterator].ToString() :
  109. "null");
  110. parameterIterator++;
  111. }
  112. //remove the "," at the end of the line
  113. if (message.EndsWith(", ")) { message = message.Substring(0, message.Length - 2); }
  114. message += ") : entering...";
  115. WriteEntry(message, TraceEventType.Verbose);
  116. }
  117. public void WriteFunctionExit()
  118. {
  119. string message = string.Empty;
  120. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  121. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  122. message = string.Format("{0} : {1}(", callingClass, callingMethod);
  123. System.Reflection.ParameterInfo[] parameterInfos = new StackFrame(1, true).GetMethod().GetParameters();
  124. foreach (System.Reflection.ParameterInfo parameterInfo in parameterInfos)
  125. {
  126. message += parameterInfo.ParameterType.Name + " " + parameterInfo.Name + ", ";
  127. }
  128. //remove the "," at the end of the line
  129. if (message.EndsWith(", ")) { message = message.Substring(0, message.Length - 2); }
  130. message += ") : leaving...";
  131. WriteEntry(message, TraceEventType.Verbose);
  132. }
  133. public void WriteFunctionExit(object returnValue)
  134. {
  135. string message = string.Empty;
  136. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  137. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  138. message = string.Format("{0} : {1}(", callingClass, callingMethod);
  139. System.Reflection.ParameterInfo[] parameterInfos = new StackFrame(1, true).GetMethod().GetParameters();
  140. foreach (System.Reflection.ParameterInfo parameterInfo in parameterInfos)
  141. {
  142. message += parameterInfo.ParameterType.Name + " " + parameterInfo.Name + ", ";
  143. }
  144. //remove the "," at the end of the line
  145. if (message.EndsWith(", ")) { message = message.Substring(0, message.Length - 2); }
  146. message += ") : leaving... ReturnValue is " + returnValue.ToString();
  147. WriteEntry(message, TraceEventType.Verbose);
  148. }
  149. public void WriteFunctionExitWithError(Exception ex)
  150. {
  151. string message = string.Empty;
  152. string error = ex.GetType().Name;
  153. if (!string.IsNullOrEmpty(error))
  154. {
  155. error += ": " + ex.Message;
  156. }
  157. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  158. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  159. message = string.Format("{0} : {1}(", callingClass, callingMethod);
  160. System.Reflection.ParameterInfo[] parameterInfos = new StackFrame(1, true).GetMethod().GetParameters();
  161. foreach (System.Reflection.ParameterInfo parameterInfo in parameterInfos)
  162. {
  163. message += parameterInfo.ParameterType.Name + " " + parameterInfo.Name + ", ";
  164. }
  165. //remove the "," at the end of the line
  166. if (message.EndsWith(", ")) { message = message.Substring(0, message.Length - 2); }
  167. message += ") : leaving with error '" + error + "'";
  168. WriteEntry(message, TraceEventType.Error);
  169. }
  170. private new void WriteEntry(string message, TraceEventType severity)
  171. {
  172. message = string.Format("{0}:{1}:{2}.{3};{4}",
  173. DateTime.Now.Hour,
  174. DateTime.Now.Minute,
  175. DateTime.Now.Second,
  176. DateTime.Now.Millisecond,
  177. message);
  178. base.WriteEntry(message, severity);
  179. }
  180. private new void WriteEntry(string message)
  181. {
  182. WriteEntry(message, TraceEventType.Information);
  183. }
  184. public void WriteEntry(string message, TraceEventType severity, params object[] args)
  185. {
  186. string callingMethod = new StackFrame(1, true).GetMethod().Name;
  187. string callingClass = new StackFrame(1, true).GetMethod().ReflectedType.Name;
  188. message = string.Format(message, args);
  189. string messageHeader = string.Format("{0} : {1}", callingClass, callingMethod);
  190. message = string.Format("{0};{1}", messageHeader, message);
  191. WriteEntry(message, severity);
  192. }
  193. private void WriteEntry(string message, params object[] args)
  194. {
  195. message = string.Format(message, args);
  196. WriteEntry(message, TraceEventType.Information);
  197. }
  198. }
  199. }
  200. #endregion Log
  201. }