/// <summary> /// Creates a logger that can log both to Console or file. /// </summary> /// <param name="AFileName">File to which the output should be written if logging to the logfile is requested.</param> /// <param name="ASuppressDateAndTime">Set to true to suppress the logging of date and time in log files (default= false).</param> public TLogging(String AFileName, bool ASuppressDateAndTime = false) { if (Path.GetFullPath(AFileName) == TLogWriter.GetLogFileName()) { return; } TLogging.Context = ""; if (ULogWriter == null) { ULogWriter = new TLogWriter(AFileName); ULogWriter.SuppressDateAndTime = ASuppressDateAndTime; ULogFileName = AFileName; } else { throw new Exception("TLogging.Create: only use one log file at the time! old name: " + TLogWriter.GetLogFileName() + "; new name: " + Path.GetFullPath(AFileName)); } TLogging.StatusBarProcedure = null; StatusBarProcedureValid = false; }
/// reset the static variables for each Web Request call. public static void ResetStaticVariables() { DebugLevel = 0; FNoLoggingToConsoleError = false; ULogWriter = null; ULogFileName = String.Empty; UUserNamePrefix = DEFAULTUSERNAMEPREFIX; ULogTextAsString = null; ULogPageNumber = 1; UNewMessageCallback = null; Context = String.Empty; StatusBarProcedure = null; StatusBarProcedureValid = false; }
/// <summary> /// Logs a message. Output destination can be selected with the Loggingtype flag. /// </summary> /// <param name="Text">Log message</param> /// <param name="ALoggingType">Determines the output destination. /// Note: More than one output destination can be chosen!</param> public static void Log(string Text, TLoggingType ALoggingType) { if (((ALoggingType & TLoggingType.ToConsole) != 0) || ((ALoggingType & TLoggingType.ToLogfile) != 0) // only in Debugmode write the messages for the statusbar also on the console (e.g. reporting progress) || (((ALoggingType & TLoggingType.ToStatusBar) != 0) && (TLogging.DebugLevel == TLogging.DEBUGLEVEL_TRACE))) { Console.Error.WriteLine(Utilities.CurrentTime() + " " + Text); if ((TLogging.Context != null) && (TLogging.Context.Length != 0)) { Console.Error.WriteLine(" Context: " + TLogging.Context); } } if (((ALoggingType & TLoggingType.ToConsole) != 0) || ((ALoggingType & TLoggingType.ToLogfile) != 0) || ((ALoggingType & TLoggingType.ToStatusBar) != 0)) { if (TLogging.StatusBarProcedureValid && (Text.IndexOf("SELECT") == -1)) { // don't print sql statements to the statusbar in debug mode if (TLogging.Context.Length != 0) { Text += "; Context: " + TLogging.Context; } StatusBarProcedure(Text); } } if ((ALoggingType & TLoggingType.ToLogfile) != 0) { if (ULogWriter != null) { TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } } else { // I found it was better to write the actual logging message, // even if the logwriter is not setup up correctly new TLogging("temp.log"); TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } ULogWriter = null; ULogFileName = null; // now throw an exception, because it is not supposed to work like this throw new ENoLoggingToFile_WrongConstructorUsedException(); } } }
/// <summary> /// Logs a message. Output destination can be selected with the Loggingtype flag. /// </summary> /// <param name="Text">Log message</param> /// <param name="ALoggingType">Determines the output destination. /// Note: More than one output destination can be chosen!</param> /// <param name="ACustomStatusCallbackProcedure">Optional instance of a custom callback procedure for writing /// to the StatusBar of a Form (default = null).</param> public static void Log(string Text, TLoggingType ALoggingType, TStatusCallbackProcedure ACustomStatusCallbackProcedure = null) { if (((ALoggingType & TLoggingType.ToConsole) != 0) && (ULogTextAsString != null) && (ULogTextAsString.Length > 0)) { // log to static string for Windows Forms app if (ULogTextAsString.Length > 16384) { TruncateLogString(8192); } ULogTextAsString += (Utilities.CurrentTime() + " " + Text + Environment.NewLine); if ((TLogging.Context != null) && (TLogging.Context.Length != 0)) { ULogTextAsString += (" Context: " + TLogging.Context + Environment.NewLine); } // Tell our caller that there is a new message if (UNewMessageCallback != null) { // Can this fail if the program has closed?? UNewMessageCallback(); } } else if (!FNoLoggingToConsoleError) { try { if (((ALoggingType & TLoggingType.ToConsole) != 0) // only in Debugmode write the messages for the statusbar also on the console (e.g. reporting progress) || (((ALoggingType & TLoggingType.ToStatusBar) != 0) && (TLogging.DebugLevel != 0))) { Console.Error.WriteLine(Utilities.CurrentTime() + " " + Text); if (!string.IsNullOrEmpty(TLogging.Context)) { Console.Error.WriteLine(" Context: " + TLogging.Context); } } } catch (System.NotSupportedException) { // ignore this exception: System.NotSupportedException: Stream does not support writing } } if (((ALoggingType & TLoggingType.ToStatusBar) != 0) && (Text.IndexOf("SELECT") == -1)) // Don't print sql statements to the statusbar { if (!string.IsNullOrEmpty(TLogging.Context)) { Text += "; Context: " + TLogging.Context; } if (ACustomStatusCallbackProcedure == null) { if (TLogging.StatusBarProcedureValid) { StatusBarProcedure(Text); } } else { ACustomStatusCallbackProcedure(Text); } } if ((ALoggingType & TLoggingType.ToLogfile) != 0) { if (ULogWriter != null) { TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } } else { // I found it was better to write the actual logging message, // even if the logwriter is not setup up correctly new TLogging("temp.log"); TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } ULogWriter = null; ULogFileName = null; // now throw an exception, because it is not supposed to work like this throw new ENoLoggingToFile_WrongConstructorUsedException(); } } }
/// <summary> /// Logs a message. Output destination can be selected with the Loggingtype flag. /// </summary> /// <param name="Text">Log message</param> /// <param name="ALoggingType">Determines the output destination. /// Note: More than one output destination can be chosen!</param> public static void Log(string Text, TLoggingType ALoggingType) { if (((ALoggingType & TLoggingType.ToConsole) != 0) && (ULogTextAsString != null) && (ULogTextAsString.Length > 0)) { // log to static string for Windows Forms app if (ULogTextAsString.Length > 16384) { TruncateLogString(8192); } ULogTextAsString += (Utilities.CurrentTime() + " " + Text + Environment.NewLine); if ((TLogging.Context != null) && (TLogging.Context.Length != 0)) { ULogTextAsString += (" Context: " + TLogging.Context + Environment.NewLine); } // Tell our caller that there is a new message if (UNewMessageCallback != null) { // Can this fail if the program has closed?? UNewMessageCallback(); } } else if (((ALoggingType & TLoggingType.ToConsole) != 0) || ((ALoggingType & TLoggingType.ToLogfile) != 0) // only in Debugmode write the messages for the statusbar also on the console (e.g. reporting progress) || (((ALoggingType & TLoggingType.ToStatusBar) != 0) && (TLogging.DebugLevel == TLogging.DEBUGLEVEL_TRACE))) { Console.Error.WriteLine(Utilities.CurrentTime() + " " + Text); if ((TLogging.Context != null) && (TLogging.Context.Length != 0)) { Console.Error.WriteLine(" Context: " + TLogging.Context); } } if (((ALoggingType & TLoggingType.ToConsole) != 0) || ((ALoggingType & TLoggingType.ToLogfile) != 0) || ((ALoggingType & TLoggingType.ToStatusBar) != 0)) { if (TLogging.StatusBarProcedureValid && (Text.IndexOf("SELECT") == -1)) { // don't print sql statements to the statusbar in debug mode if (TLogging.Context.Length != 0) { Text += "; Context: " + TLogging.Context; } StatusBarProcedure(Text); } } if ((ALoggingType & TLoggingType.ToLogfile) != 0) { if (ULogWriter != null) { TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } } else { // I found it was better to write the actual logging message, // even if the logwriter is not setup up correctly new TLogging("temp.log"); TLogWriter.Log(Text); if (TLogging.Context.Length != 0) { TLogWriter.Log(" Context: " + TLogging.Context); } ULogWriter = null; ULogFileName = null; // now throw an exception, because it is not supposed to work like this throw new ENoLoggingToFile_WrongConstructorUsedException(); } } }