/// <summary> /// Pushes the message info to a database table entry /// </summary> /// <param name="message"><c>LogMessage</c> that is to be sent to the database</param> public override void Publish( LogMessage message) { IDbCommand cmd = null; try { // Call the method to create the database connection this.GetDBConnection(); // Open the database connection this.OpenDBConnection(); // Create the command for saving the log entry cmd = this.CreateDBCommand(message); // Write the log entry to the database this.WriteLog(cmd); // Close the database connection this.Close(); } catch (Exception) { // Possible errors: } }
/// <summary> /// Abstract method to create the database command for storing the log entry in the database /// </summary> /// <param name="message"><c>LogMessage</c> to be stored</param> /// <returns><c>IDbCommand</c> populated with the necessary information to save the log entry /// to the database</returns> /// <remarks>Use the connection stored in the <c>conn</c> class variable. Make sure to check /// for null instances before creating the command.</remarks> protected abstract IDbCommand CreateDBCommand(LogMessage message);
/// <summary> /// Pushes the message info to a file/buffer /// </summary> /// <param name="message"><c>LogMessage</c> that is to be sent to the data source/buffer</param> /// <returns></returns> public override void Publish( LogMessage message) { try { // Uses thread-safe wrapper this.stream = TextWriter.Synchronized(File.AppendText(this.fileName)); this.stream.Write("{0} [{1}] : ", message.MessageTime, message.Level); this.stream.WriteLine(message.Message); } catch (Exception) { // Possible errors: System.IO.DirectoryNotFoundException, System.IO.IOException } finally { this.Flush(); this.Close(); } }
/// <summary> /// Adds the message to the <c>Queue</c> and triggers an event to process the queue. /// </summary> /// <param name="message"><c>LogMessage</c> object to validate</param> /// <returns></returns> private void QueueMessage( LogMessage message) { // Lock queue for thread-safety lock (this.messageQueue) { try { this.messageQueue.Enqueue(message); } catch { // do we want to do something with this?? } } }
/// <summary> /// Performs level validation testing message level against Logger level, and applies the filter. /// </summary> /// <param name="message"><c>LogMessage</c> object to validate</param> /// <returns><b>bool</b> indicating if the <c>LogMessage</c> can be published according to its level</returns> private bool IsLoggable( LogMessage message) { bool returnVal = false; try { returnVal = ((message.Level >= this.level) && (this.handlers.Count > 0)); // Continue check if return value is currently true if (returnVal) { // Check filter object if (this.filter != null) { // Test if filter allows message to pass if (!this.filter.IsLoggable(message)) { returnVal = false; } } } } catch {} return returnVal; }
/// <summary> /// Performs level validation for message and if it passes, adds it to the message Queue. /// </summary> /// <param name="level"><c>MessageLevel</c> that specifies the level of the message.</param> /// <param name="message"><c>String</c> that specifies the message text.</param> /// <param name="exception"><c>Exception</c> that specifies the error that occured.</param> /// <param name="eventArg">An optional reference to context information about the /// origin of the message to log. Pass null if no context information needed.</param> /// <param name="parms">Additional content to be included in the message.</param> public void Log( MessageLevel level, string message, Exception exception, object eventArg, params object[] parms) { LogMessage logMessage = new LogMessage(message, level); // Continue if the message passes logging criteria if (this.IsLoggable(logMessage)) { // Log exception info if applicable if (exception != null) { logMessage.Message += " : " + exception.Message; logMessage.Message += Environment.NewLine + " " + exception.StackTrace; if (this.addInnerExceptions && exception.InnerException != null) { logMessage.Message += Environment.NewLine + "The following inner exceptions occurred:" + Environment.NewLine; while (exception.InnerException != null) { logMessage.Message += exception.InnerException.ToString(); exception = exception.InnerException; } } } // if an eventArg value was supplied, set it in the message if (eventArg != null) { logMessage.EventArg = eventArg; } // Log all param elements if (parms != null) { if (parms.Length > 0) { logMessage.Message += Environment.NewLine + " Params= "; foreach (object item in parms) { if (item != null) { logMessage.Message += item + " "; } } } } this.QueueMessage(logMessage); } logMessage = null; }