1 // created on 07/06/2002 at 09:34
3 // Npgsql.NpgsqlEventLog.cs
6 // Dave Page (dpage@postgresql.org)
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
29 using System
.Diagnostics
;
31 using System
.Resources
;
37 /// The level of verbosity of the NpgsqlEventLog
39 public enum LogLevel
{
45 /// Only log the most common issues
56 /// This class handles all the Npgsql event and debug logging
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()
71 static NpgsqlEventLog()
73 LogResMan
= new ResourceManager(typeof(NpgsqlEventLog
));
77 /// Sets/Returns the level of information to log to the logfile.
79 /// <value>The current <see cref="Npgsql.LogLevel">LogLevel</see></value>
80 public static LogLevel Level
{
83 LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "LogLevel");
88 LogPropertySet(LogLevel
.Debug
, CLASSNAME
, "LogLevel", value);
94 /// Sets/Returns the filename to use for logging.
96 /// <value>The filename of the current Log file.</value>
97 public static String LogName
{
100 LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "LogName");
105 LogPropertySet(LogLevel
.Normal
, CLASSNAME
, "LogName", value);
111 /// Sets/Returns whether Log messages should be echoed to the console
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");
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.
130 /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
133 /// This method is obsolete and should no longer be used.
134 /// It is likely to be removed in future versions of Npgsql
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
)
143 Process proc
= Process
.GetCurrentProcess();
147 Console
.WriteLine(message
);
155 StreamWriter writer
= new StreamWriter(logfile
, true);
157 // The format of the logfile is
158 // [Date] [Time] [PID] [Level] [Message]
159 writer
.WriteLine(System
.DateTime
.Now
+ "\t" + proc
.Id
+ "\t" + msglevel
+ "\t" + message
);
166 /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
168 /// <param name="resman">The <see cref="System.Resources.ResourceManager">ResourceManager</see> to get the localized resources</param>
169 /// <param name="ResourceString">The name of the resource that should be fetched by the <see cref="System.Resources.ResourceManager">ResourceManager</see></param>
170 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
171 /// <param name="Parameters">The additional parameters that shall be included into the log-message (must be compatible with the string in the resource):</param>
172 internal static void LogMsg(ResourceManager resman
, string ResourceString
, LogLevel msglevel
, params Object
[] Parameters
)
174 if (msglevel
> level
)
177 string message
= resman
.GetString(ResourceString
);
179 if (message
== null) {
180 message
= String
.Format("Unable to find resource string {0} for class {1}", ResourceString
, resman
.BaseName
);
181 } else if (Parameters
.Length
> 0) {
182 message
= String
.Format(message
, Parameters
);
185 LogMsg(message
, msglevel
);
189 /// Writes the default log-message for the action of calling the Get-part of an Indexer to the log file.
191 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
192 /// <param name="ClassName">The name of the class that contains the Indexer</param>
193 /// <param name="IndexerParam">The parameter given to the Indexer</param>
194 internal static void LogIndexerGet(LogLevel msglevel
, string ClassName
, object IndexerParam
)
196 if (msglevel
> level
)
198 string message
= String
.Format(LogResMan
.GetString("Indexer_Get"), ClassName
, IndexerParam
);
199 LogMsg(message
, msglevel
);
203 /// Writes the default log-message for the action of calling the Set-part of an Indexer to the logfile.
205 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
206 /// <param name="ClassName">The name of the class that contains the Indexer</param>
207 /// <param name="IndexerParam">The parameter given to the Indexer</param>
208 /// <param name="value">The value the Indexer is set to</param>
209 internal static void LogIndexerSet(LogLevel msglevel
, string ClassName
, object IndexerParam
, object value)
211 if (msglevel
> level
)
213 string message
= String
.Format(LogResMan
.GetString("Indexer_Set"), ClassName
, IndexerParam
, value);
214 LogMsg(message
, msglevel
);
218 /// Writes the default log-message for the action of calling the Get-part of a Property to the logfile.
220 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
221 /// <param name="ClassName">The name of the class that contains the Property</param>
222 /// <param name="PropertyName">The name of the Property</param>
223 internal static void LogPropertyGet(LogLevel msglevel
, string ClassName
, string PropertyName
)
225 if (msglevel
> level
)
227 string message
= String
.Format(LogResMan
.GetString("Property_Get"), ClassName
, PropertyName
);
228 LogMsg(message
, msglevel
);
232 /// Writes the default log-message for the action of calling the Set-part of a Property to the logfile.
234 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
235 /// <param name="ClassName">The name of the class that contains the Property</param>
236 /// <param name="PropertyName">The name of the Property</param>
237 /// <param name="value">The value the Property is set to</param>
238 internal static void LogPropertySet(LogLevel msglevel
, string ClassName
, string PropertyName
, object value)
240 if (msglevel
> level
)
242 string message
= String
.Format(LogResMan
.GetString("Property_Set"), ClassName
, PropertyName
, value);
243 LogMsg(message
, msglevel
);
247 /// Writes the default log-message for the action of calling a Method without Arguments to the logfile.
249 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
250 /// <param name="ClassName">The name of the class that contains the Method</param>
251 /// <param name="MethodName">The name of the Method</param>
252 internal static void LogMethodEnter(LogLevel msglevel
, string ClassName
, string MethodName
)
254 if (msglevel
> level
)
256 string message
= String
.Format(LogResMan
.GetString("Method_0P_Enter"), ClassName
, MethodName
);
257 LogMsg(message
, msglevel
);
261 /// Writes the default log-message for the action of calling a Method with one Argument to the logfile.
263 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
264 /// <param name="ClassName">The name of the class that contains the Method</param>
265 /// <param name="MethodName">The name of the Method</param>
266 /// <param name="MethodParameter">The value of the Argument of the Method</param>
267 internal static void LogMethodEnter(LogLevel msglevel
, string ClassName
, string MethodName
, object MethodParameter
)
269 if (msglevel
> level
)
271 string message
= String
.Format(LogResMan
.GetString("Method_1P_Enter"), ClassName
, MethodName
, MethodParameter
);
272 LogMsg(message
, msglevel
);
276 /// Writes the default log-message for the action of calling a Method with two Arguments to the logfile.
278 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
279 /// <param name="ClassName">The name of the class that contains the Method</param>
280 /// <param name="MethodName">The name of the Method</param>
281 /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
282 /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
283 internal static void LogMethodEnter(LogLevel msglevel
, string ClassName
, string MethodName
, object MethodParameter1
, object MethodParameter2
)
285 if (msglevel
> level
)
287 string message
= String
.Format(LogResMan
.GetString("Method_2P_Enter"), ClassName
, MethodName
, MethodParameter1
, MethodParameter2
);
288 LogMsg(message
, msglevel
);
292 /// Writes the default log-message for the action of calling a Method with three Arguments to the logfile.
294 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
295 /// <param name="ClassName">The name of the class that contains the Method</param>
296 /// <param name="MethodName">The name of the Method</param>
297 /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
298 /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
299 /// <param name="MethodParameter3">The value of the third Argument of the Method</param>
300 internal static void LogMethodEnter(LogLevel msglevel
, string ClassName
, string MethodName
, object MethodParameter1
, object MethodParameter2
, object MethodParameter3
)
302 if (msglevel
> level
)
304 string message
= String
.Format(LogResMan
.GetString("Method_3P_Enter"), ClassName
, MethodName
, MethodParameter1
, MethodParameter2
, MethodParameter3
);
305 LogMsg(message
, msglevel
);
309 /// Writes the default log-message for the action of calling a Method with more than three Arguments to the logfile.
311 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
312 /// <param name="ClassName">The name of the class that contains the Method</param>
313 /// <param name="MethodName">The name of the Method</param>
314 /// <param name="MethodParameters">A <see cref="System.Object">Object</see>-Array with zero or more Ojects that are Arguments of the Method.</param>
315 internal static void LogMethodEnter(LogLevel msglevel
, string ClassName
, string MethodName
, params object[] MethodParameters
)
317 if (msglevel
> level
)
319 string message
= String
.Empty
;
320 switch (MethodParameters
.Length
)
323 message
= String
.Format(LogResMan
.GetString("Method_4P_Enter"), ClassName
, MethodName
,
324 MethodParameters
[0], MethodParameters
[1], MethodParameters
[2], MethodParameters
[3]);
327 message
= String
.Format(LogResMan
.GetString("Method_5P_Enter"), ClassName
, MethodName
,
328 MethodParameters
[0], MethodParameters
[1], MethodParameters
[2], MethodParameters
[3], MethodParameters
[4]);
331 message
= String
.Format(LogResMan
.GetString("Method_6P_Enter"), ClassName
, MethodName
,
332 MethodParameters
[0], MethodParameters
[1], MethodParameters
[2], MethodParameters
[3], MethodParameters
[4], MethodParameters
[5]);
335 // should always be true - but who knows ;-)
336 if (MethodParameters
.Length
> 6)
337 message
= String
.Format(LogResMan
.GetString("Method_6P+_Enter"), ClassName
, MethodName
, MethodParameters
[0], MethodParameters
[1]);
340 LogMsg(message
, msglevel
);