NpgsqlEventLog.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // created on 07/06/2002 at 09:34
  2. // Npgsql.NpgsqlEventLog.cs
  3. //
  4. // Author:
  5. // Dave Page ([email protected])
  6. //
  7. // Copyright (C) 2002 The Npgsql Development Team
  8. // [email protected]
  9. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  10. //
  11. //
  12. // This library is free software; you can redistribute it and/or
  13. // modify it under the terms of the GNU Lesser General Public
  14. // License as published by the Free Software Foundation; either
  15. // version 2.1 of the License, or (at your option) any later version.
  16. //
  17. // This library is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. // Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public
  23. // License along with this library; if not, write to the Free Software
  24. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. using System.IO;
  26. using System.Text;
  27. using System.Diagnostics;
  28. using System;
  29. using System.Resources;
  30. namespace Npgsql
  31. {
  32. /// <summary>
  33. /// The level of verbosity of the NpgsqlEventLog
  34. /// </summary>
  35. public enum LogLevel {
  36. /// <summary>
  37. /// Don't log at all
  38. /// </summary>
  39. None = 0,
  40. /// <summary>
  41. /// Only log the most common issues
  42. /// </summary>
  43. Normal = 1,
  44. /// <summary>
  45. /// Log everything
  46. /// </summary>
  47. Debug = 2
  48. }
  49. /// <summary>
  50. /// This class handles all the Npgsql event and debug logging
  51. /// </summary>
  52. public class NpgsqlEventLog
  53. {
  54. // Logging related values
  55. private static readonly String CLASSNAME = "NpgsqlEventLog";
  56. private static String logfile;
  57. private static LogLevel level;
  58. private static Boolean echomessages;
  59. private static ResourceManager LogResMan;
  60. private NpgsqlEventLog()
  61. {}
  62. // static constructor
  63. static NpgsqlEventLog()
  64. {
  65. LogResMan = new ResourceManager(typeof(NpgsqlEventLog));
  66. }
  67. ///<summary>
  68. /// Sets/Returns the level of information to log to the logfile.
  69. /// </summary>
  70. /// <value>The current <see cref="Npgsql.LogLevel">LogLevel</see></value>
  71. public static LogLevel Level {
  72. get
  73. {
  74. LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogLevel");
  75. return level;
  76. }
  77. set
  78. {
  79. LogPropertySet(LogLevel.Debug, CLASSNAME, "LogLevel", value);
  80. level = value;
  81. }
  82. }
  83. ///<summary>
  84. /// Sets/Returns the filename to use for logging.
  85. /// </summary>
  86. /// <value>The filename of the current Log file.</value>
  87. public static String LogName {
  88. get
  89. {
  90. LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogName");
  91. return logfile;
  92. }
  93. set
  94. {
  95. LogPropertySet(LogLevel.Normal, CLASSNAME, "LogName", value);
  96. logfile = value;
  97. }
  98. }
  99. ///<summary>
  100. /// Sets/Returns whether Log messages should be echoed to the console
  101. /// </summary>
  102. /// <value><b>true</b> if Log messages are echoed to the console, otherwise <b>false</b></value>
  103. public static Boolean EchoMessages {
  104. get
  105. {
  106. LogPropertyGet(LogLevel.Debug, CLASSNAME, "EchoMessages");
  107. return echomessages;
  108. }
  109. set
  110. {
  111. LogPropertySet(LogLevel.Normal, CLASSNAME, "EchoMessages", value);
  112. echomessages = value;
  113. }
  114. }
  115. // Event/Debug Logging
  116. // This method should be private and only used by the internal methods that support localization.
  117. /// <summary>
  118. /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
  119. /// </summary>
  120. /// <remarks>
  121. /// This method is obsolete and should no longer be used.
  122. /// It is likely to be removed in future versions of Npgsql
  123. /// </remarks>
  124. /// <param name="message">The message to write to the event log</param>
  125. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  126. private static void LogMsg(String message, LogLevel msglevel)
  127. {
  128. if (msglevel > level)
  129. return;
  130. Process proc = Process.GetCurrentProcess();
  131. if (echomessages)
  132. {
  133. Console.WriteLine(message);
  134. }
  135. if (logfile != null)
  136. {
  137. if (logfile != "")
  138. {
  139. StreamWriter writer = new StreamWriter(logfile, true);
  140. // The format of the logfile is
  141. // [Date] [Time] [PID] [Level] [Message]
  142. writer.WriteLine(System.DateTime.Now + "\t" + proc.Id + "\t" + msglevel + "\t" + message);
  143. writer.Close();
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
  149. /// </summary>
  150. /// <param name="resman">The <see cref="System.Resources.ResourceManager">ResourceManager</see> to get the localized resources</param>
  151. /// <param name="ResourceString">The name of the resource that should be fetched by the <see cref="System.Resources.ResourceManager">ResourceManager</see></param>
  152. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  153. /// <param name="Parameters">The additional parameters that shall be included into the log-message (must be compatible with the string in the resource):</param>
  154. internal static void LogMsg(ResourceManager resman, string ResourceString, LogLevel msglevel, params Object[] Parameters)
  155. {
  156. if (msglevel > level)
  157. return;
  158. string message = resman.GetString(ResourceString);
  159. if (message == null) {
  160. message = String.Format("Unable to find resource string {0} for class {1}", ResourceString, resman.BaseName);
  161. } else if (Parameters.Length > 0) {
  162. message = String.Format(message, Parameters);
  163. }
  164. LogMsg(message, msglevel);
  165. }
  166. /// <summary>
  167. /// Writes the default log-message for the action of calling the Get-part of an Indexer to the log file.
  168. /// </summary>
  169. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  170. /// <param name="ClassName">The name of the class that contains the Indexer</param>
  171. /// <param name="IndexerParam">The parameter given to the Indexer</param>
  172. internal static void LogIndexerGet(LogLevel msglevel, string ClassName, object IndexerParam)
  173. {
  174. if (msglevel > level)
  175. return;
  176. string message = String.Format(LogResMan.GetString("Indexer_Get"), ClassName, IndexerParam);
  177. LogMsg(message, msglevel);
  178. }
  179. /// <summary>
  180. /// Writes the default log-message for the action of calling the Set-part of an Indexer to the logfile.
  181. /// </summary>
  182. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  183. /// <param name="ClassName">The name of the class that contains the Indexer</param>
  184. /// <param name="IndexerParam">The parameter given to the Indexer</param>
  185. /// <param name="value">The value the Indexer is set to</param>
  186. internal static void LogIndexerSet(LogLevel msglevel, string ClassName, object IndexerParam, object value)
  187. {
  188. if (msglevel > level)
  189. return;
  190. string message = String.Format(LogResMan.GetString("Indexer_Set"), ClassName, IndexerParam, value);
  191. LogMsg(message, msglevel);
  192. }
  193. /// <summary>
  194. /// Writes the default log-message for the action of calling the Get-part of a Property to the logfile.
  195. /// </summary>
  196. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  197. /// <param name="ClassName">The name of the class that contains the Property</param>
  198. /// <param name="PropertyName">The name of the Property</param>
  199. internal static void LogPropertyGet(LogLevel msglevel, string ClassName, string PropertyName)
  200. {
  201. if (msglevel > level)
  202. return;
  203. string message = String.Format(LogResMan.GetString("Property_Get"), ClassName, PropertyName);
  204. LogMsg(message, msglevel);
  205. }
  206. /// <summary>
  207. /// Writes the default log-message for the action of calling the Set-part of a Property to the logfile.
  208. /// </summary>
  209. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  210. /// <param name="ClassName">The name of the class that contains the Property</param>
  211. /// <param name="PropertyName">The name of the Property</param>
  212. /// <param name="value">The value the Property is set to</param>
  213. internal static void LogPropertySet(LogLevel msglevel, string ClassName, string PropertyName, object value)
  214. {
  215. if (msglevel > level)
  216. return;
  217. string message = String.Format(LogResMan.GetString("Property_Set"), ClassName, PropertyName, value);
  218. LogMsg(message, msglevel);
  219. }
  220. /// <summary>
  221. /// Writes the default log-message for the action of calling a Method without Arguments to the logfile.
  222. /// </summary>
  223. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  224. /// <param name="ClassName">The name of the class that contains the Method</param>
  225. /// <param name="MethodName">The name of the Method</param>
  226. internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName)
  227. {
  228. if (msglevel > level)
  229. return;
  230. string message = String.Format(LogResMan.GetString("Method_0P_Enter"), ClassName, MethodName);
  231. LogMsg(message, msglevel);
  232. }
  233. /// <summary>
  234. /// Writes the default log-message for the action of calling a Method with one Argument to the logfile.
  235. /// </summary>
  236. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  237. /// <param name="ClassName">The name of the class that contains the Method</param>
  238. /// <param name="MethodName">The name of the Method</param>
  239. /// <param name="MethodParameter">The value of the Argument of the Method</param>
  240. internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter)
  241. {
  242. if (msglevel > level)
  243. return;
  244. string message = String.Format(LogResMan.GetString("Method_1P_Enter"), ClassName, MethodName, MethodParameter);
  245. LogMsg(message, msglevel);
  246. }
  247. /// <summary>
  248. /// Writes the default log-message for the action of calling a Method with two Arguments to the logfile.
  249. /// </summary>
  250. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  251. /// <param name="ClassName">The name of the class that contains the Method</param>
  252. /// <param name="MethodName">The name of the Method</param>
  253. /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
  254. /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
  255. internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2)
  256. {
  257. if (msglevel > level)
  258. return;
  259. string message = String.Format(LogResMan.GetString("Method_2P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2);
  260. LogMsg(message, msglevel);
  261. }
  262. /// <summary>
  263. /// Writes the default log-message for the action of calling a Method with three Arguments to the logfile.
  264. /// </summary>
  265. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  266. /// <param name="ClassName">The name of the class that contains the Method</param>
  267. /// <param name="MethodName">The name of the Method</param>
  268. /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
  269. /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
  270. /// <param name="MethodParameter3">The value of the third Argument of the Method</param>
  271. internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2, object MethodParameter3)
  272. {
  273. if (msglevel > level)
  274. return;
  275. string message = String.Format(LogResMan.GetString("Method_3P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2, MethodParameter3);
  276. LogMsg(message, msglevel);
  277. }
  278. /// <summary>
  279. /// Writes the default log-message for the action of calling a Method with more than three Arguments to the logfile.
  280. /// </summary>
  281. /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
  282. /// <param name="ClassName">The name of the class that contains the Method</param>
  283. /// <param name="MethodName">The name of the Method</param>
  284. /// <param name="MethodParameters">A <see cref="System.Object">Object</see>-Array with zero or more Ojects that are Arguments of the Method.</param>
  285. internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, params object[] MethodParameters)
  286. {
  287. if (msglevel > level)
  288. return;
  289. string message = String.Empty;
  290. switch (MethodParameters.Length)
  291. {
  292. case 4:
  293. message = String.Format(LogResMan.GetString("Method_4P_Enter"), ClassName, MethodName,
  294. MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3]);
  295. break;
  296. case 5:
  297. message = String.Format(LogResMan.GetString("Method_5P_Enter"), ClassName, MethodName,
  298. MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4]);
  299. break;
  300. case 6:
  301. message = String.Format(LogResMan.GetString("Method_6P_Enter"), ClassName, MethodName,
  302. MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4], MethodParameters[5]);
  303. break;
  304. default:
  305. // should always be true - but who knows ;-)
  306. if (MethodParameters.Length > 6)
  307. message = String.Format(LogResMan.GetString("Method_6P+_Enter"), ClassName, MethodName, MethodParameters[0], MethodParameters[1]);
  308. break;
  309. }
  310. LogMsg(message, msglevel);
  311. }
  312. }
  313. }