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