protected override void SubmitMessageQueue(LoggerEventArgs[] messages) { // Just in case if there are many mesages... foreach (LoggerEventArgs e in messages) { String message = e.Message; // If message fits in one line then print it as it is if (message.Length < _splitPoint) WriteMessageLine(e.TimeStamp, e.Tag, e.Message); else { // Message too long, split it into multiple lines Boolean firstLine = true; while (message.Length != 0) { Int32 split = message.Substring(0, message.Length > _splitPoint ? _splitPoint : message.Length).LastIndexOf(' '); if (split < 0) split = message.Length > _splitPoint ? _splitPoint : message.Length; String msgLine = message.Substring(0, split); message = message.Substring(message.Length > split + 1 ? split + 1 : message.Length); if (firstLine) { WriteMessageLine(e.TimeStamp, e.Tag, msgLine); firstLine = false; } else WriteMessageLine(null, null, msgLine); } } } }
/// <summary> /// Sends a log message to all attached loggers /// </summary> /// <param name="tag">Tag associated with the message</param> /// <param name="format">Format string of the message</param> /// <param name="args">Message format arguments</param> public static void SendLogMessage(String tag, String format, params Object[] args) { if (_onLogMessage != null) { LoggerEventArgs e = new LoggerEventArgs(tag, format, args); _onLogMessage(null, e); } }
protected override void SubmitMessageQueue(LoggerEventArgs[] messages) { foreach (LoggerEventArgs e in messages) { DateTime dt = new DateTime(e.TimeStamp * 10000); String time = String.Format("{0}:{1}:{2}.{3}", dt.Hour, dt.Minute, dt.Second, dt.Millisecond); _writer.WriteLine("{0,-20}{1,-20}{2}", time, e.Tag, e.Message); } _writer.Flush(); }
/// <summary> /// Event handler for OnLogMessage event /// </summary> /// <param name="sender">Event origin</param> /// <param name="e">Event data</param> public void HandleMessage(Object sender, LoggerEventArgs e) { EnqueueMessage(e); }
/// <summary> /// Enqueues a message for processing /// </summary> /// <param name="arg"></param> public virtual void EnqueueMessage(LoggerEventArgs arg) { _messages.Enqueue(arg); }
// Logger thread method, must remain private! private void IterateLogger() { while (true) { if (!_terminateRequest) { // Do nothing if there is no need to flush messages if (_messages.Count < _submissionBatch) continue; else { // Submit messages if needed /* A faster way of doing this might be to copy entire _messages into an array via ToArray() and clearing the queue, * but considering that there can be new messages arriving between these two instructions, copying one by one * seems more reliable. More investigation needed. */ LoggerEventArgs[] messages = new LoggerEventArgs[_submissionBatch]; for (int i = 0; i < _submissionBatch; i++) { if (!_messages.TryDequeue(out messages[i])) throw new Exception("Logger.RunLogger(): Failed to dequeue the next log message!"); } SubmitMessageQueue(messages); } } else { // If termination is requested, submit remaining messages and quit the thread SubmitMessageQueue(_messages.ToArray()); ReleaseResources(); break; } } }
/// <summary> /// When implemented, writes a collection of log messages to underlying physical storage /// </summary> /// <param name="messages">Collection of messages to write</param> protected abstract void SubmitMessageQueue(LoggerEventArgs[] messages);