Many ways to do this – the way I do it doesn’t involve the log _file_ at all, rather by creating a special appender dynamically, that populates a grid control:

 

                   private delegate void AppendDelegate(log4net.Core.LoggingEvent evt, Thread thread, log4net.Layout.ILayout layout);

                   private GridAppender gridAppender;

                   private AppendDelegate append;

 

 

                   // To implement a visual log, just use a custom log4net appender, suitably configured.

                   // This will catch all events logged and append them to a C1FlexGrid

                   log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy) LogManager.GetRepository();

                   AppendDelegate append = new AppendDelegate(Append);

                   gridAppender = new GridAppender(append);

                   gridAppender.Layout = new log4net.Layout.PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %m");

                   h.Root.AddAppender(gridAppender);

 

 

                   private void Append(log4net.Core.LoggingEvent evt, log4net.Layout.ILayout layout) {

                             if (this.IsDisposed)

                                      return;

 

                             if (!InvokeRequired) {

                                      /*

                                       * The appender will append one event at a time to the bottom of a grid control

                                       */

                                      Grid1.Redraw = false;

 

                                       StringBuilder messageBuilder = new StringBuilder();

                                      StringWriter sw = new StringWriter(messageBuilder);

                                      layout.Format(sw, evt);

                                      messageBuilder.Append(evt.GetExceptionString());

 

                                      // Build the full message and exception report if applicable

                                      string message = messageBuilder.ToString();

 

                                      // Split the message into lines

                                      string[] lines = message.Split('\n');

 

                                      if (lines.Length > 0) {

                                                // Add the first line

                                                Row newRow = Grid1.AddItem(new object[] { null, lines[0] });

 

                                                // Enforce the line limit

                                                if (Grid1.Rows.Count > lineLimit) {

                                                          // Too many lines -- roll one off

                                                          if (Grid1.Rows[0].IsNode) {

                                                                   // Roll off the entire node

                                                                   Grid1.Rows[0].Node.RemoveNode();

                                                          }

                                                          else {

                                                                   // Roll off the single item

                                                                   Grid1.RemoveItem(0);

                                                          }

                                                }

 

                                                // Set the image for the new event according to its Level

                                                Image image = null;

                                                if (evt.Level == log4net.Core.Level.Fatal)

                                                          image = logImages.Images[(int) LogLevelEnum.Fatal];

                                                else if (evt.Level == log4net.Core.Level.Error)

                                                          image = logImages.Images[(int) LogLevelEnum.Error];

                                                else if (evt.Level == log4net.Core.Level.Warn)

                                                          image = logImages.Images[(int) LogLevelEnum.Warning];

                                                else if (evt.Level == log4net.Core.Level.Info)

                                                          image = logImages.Images[(int) LogLevelEnum.Info];

                                                else if (evt.Level == log4net.Core.Level.Debug)

                                                          image = logImages.Images[(int) LogLevelEnum.Debug];

                                                else

                                                          image = null;

                                                Grid1.SetCellImage(newRow.Index, 1, image);

 

                                                newRow.IsNode = true;

 

                                                if (lines.Length > 1) {

                                                          // There are more lines -- use a tree node to expand the rest

                                                          for (int i = 1; i < lines.Length; i++) {

                                                                   if (lines[i].Trim().Length > 0) {

                                                                             Node newSubNode = newRow.Node.AddNode(NodeTypeEnum.LastChild, lines[i]);

                                                                   }

                                                          }

                                                }

                                      }

 

                                      // Auto-scroll to the end (the newest message)

                                      Grid1.Select(c1FlexGrid1.Rows.Count - 1, 1);

                                      Grid1.ShowCell(c1FlexGrid1.Rows.Count - 1, 1);

 

                                      Grid1.Redraw = true;

                             }

                             else {

                                      AppendDelegate append = new AppendDelegate(Append);

                                      this.BeginInvoke(append, new object[] { evt, layout });

                             }

                   }

 

 

                   private class GridAppender : log4net.Appender.AppenderSkeleton {

                             /*

                              * This custom log4net appender will append to a grid

                              * It has a limited number of lines (old lines will be rolled off), and holds an

                              * ImageList for the different levels of severity.

                              */

                             private AppendDelegate append;

 

                             public GridAppender(AppendDelegate append) {

                                      this.append = append;

                             }

 

                             protected override void Append(log4net.Core.LoggingEvent evt) {

                                      append(evt, Layout);

                             }

                   }

 

Wayne M. Bradney
Application Architect, Derivatives & Structured Products
Wall Street Systems
30 Broad Street, 24th Floor, New York, NY 10004 USA
Tel: +1 (212) 809 7200
Fax: +1 (212) 809 7578

Visit our website at http://www.wallstreetsystems.com

This transmission may be a privileged or confidential communication. If you are not the intended recipient, you are hereby notified that you have received this transmission in error; any review, dissemination, distribution or copying of this transmission is strictly prohibited. If you have received this communication in error, please notify us immediately by reply or by telephone (call us at +1-212-809-7200) and immediately delete this message and all its attachments.

 


From: Saurabh Dani [mailto:[EMAIL PROTECTED]
Sent: Wednesday, August 16, 2006 10:23 PM
To: [email protected]
Subject: simulating tail -f

 

Greetings,

I am writing the logs to log files using File appenders (2 different logs, one for info and one for debug).

Is it possible to access these logs and show them within my application's Interface upon clicking a "view current logs" button?

Basically I would like to simulate "tail -f" from within the application.

Thanks
Saurabh

Reply via email to