DOCUMENTATION

Include Preceding Logs into a Report

Logify Alert allows adding custom data to the generated reports. This example demonstrates how to use this feature to include 100 logs that preceded a crash into a Logify report.

Note: The example implies that you use some logging tool to collect an application's logs. In this example, the log4net tool is used. If you use any other tool, adapt the first part of the example for your approach.

Generate Logs Pool

For this purpose, use the following code.

    namespace SendLastLogsWithLogifyReport {
        static class Program {
            public static log4net.ILog Log { get; private set; }
            public static LimitedMemoryAppender LastLogEntries { get; private set; }
            [STAThread]
            static void Main() {
                XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
                Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                LastLogEntries = GetLimitedMemoryAppender();
               //...
            }
       
            static LimitedMemoryAppender GetLimitedMemoryAppender() {
                foreach (IAppender appender in Log.Logger.Repository.GetAppenders()) {
                    LimitedMemoryAppender memAppender = appender as LimitedMemoryAppender;
                    if (memAppender != null)
                        return memAppender;
                }
                return null;
            }
        }
        // Commands log4net to store the last 100 entries
        public class LimitedMemoryAppender : MemoryAppender {
            const int eventsLimit = 100;
            protected override void Append(LoggingEvent loggingEvent) {
                base.Append(loggingEvent);
                if (m_eventsList.Count > eventsLimit)
                    m_eventsList.RemoveAt(0);
            }
        }
    }

Send Logs as a Report's Custom Data

To include generated logs into a report, subscribe to and handle the Logify client's OnBeforeReportException event, as shown in the code below.

Note: The OnBeforeReportException event occurs before Logify Alert sends a new crash report to the service.

    namespace SendLastLogsWithLogifyReport {
        static class Program {
            //...
            [STAThread]
            static void Main() {
                //...
                // Sets Up Logify Alert
                LogifyAlert client = LogifyAlert.Instance;
                client.BeforeReportException += OnBeforeReportException;
                client.ApiKey = "SPECIFY_YOUR_API_KEY_HERE";
                client.StartExceptionsHandling();
            
                // Runs your application
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            // Handles the OnBeforeReportException event so it adds custom data to a report
            static void OnBeforeReportException(object sender, BeforeReportExceptionEventArgs args) {
                if (LastLogEntries == null)
                    return;
                // Converts the last 100 entries to a string and adds it to the Logify Alert report's custom data
                try {
                    StringBuilder content = new StringBuilder();
                    LoggingEvent[] events = LastLogEntries.GetEvents();
                    foreach (LoggingEvent @event in events) {
                        content.AppendLine(@event.RenderedMessage);
                    }
                    LogifyAlert.Instance.CustomData["LastLogEntries"] = content.ToString();
                }
                catch {
                }
            }
        }
    }

Entire Example

So, to perform the entire task, use the code below.

    namespace SendLastLogsWithLogifyReport {
        static class Program {
            public static log4net.ILog Log { get; private set; }
            public static LimitedMemoryAppender LastLogEntries { get; private set; }
            [STAThread]
            static void Main() {
                XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
                Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                LastLogEntries = GetLimitedMemoryAppender();
            
                // Sets Up Logify Alert
                LogifyAlert client = LogifyAlert.Instance;
                client.BeforeReportException += OnBeforeReportException;
                client.ApiKey = "SPECIFY_YOUR_API_KEY_HERE";
                client.StartExceptionsHandling();
            
                // Runs your application
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            // Handles the OnBeforeReportException event so it adds custom data to a report
            static void OnBeforeReportException(object sender, BeforeReportExceptionEventArgs args) {
                if (LastLogEntries == null)
                    return;
                // Converts the last 100 entries to a string and adds it to the Logify Alert report's custom data
                try {
                    StringBuilder content = new StringBuilder();
                    LoggingEvent[] events = LastLogEntries.GetEvents();
                    foreach (LoggingEvent @event in events) {
                        content.AppendLine(@event.RenderedMessage);
                    }
                    LogifyAlert.Instance.CustomData["LastLogEntries"] = content.ToString();
                }
                catch {
                }
            }
            static LimitedMemoryAppender GetLimitedMemoryAppender() {
                foreach (IAppender appender in Log.Logger.Repository.GetAppenders()) {
                    LimitedMemoryAppender memAppender = appender as LimitedMemoryAppender;
                    if (memAppender != null)
                        return memAppender;
                }
                return null;
            }
        }
        // Commands log4net to store the last 100 entries
        public class LimitedMemoryAppender : MemoryAppender {
            const int eventsLimit = 100;
            protected override void Append(LoggingEvent loggingEvent) {
                base.Append(loggingEvent);
                if (m_eventsList.Count > eventsLimit)
                    m_eventsList.RemoveAt(0);
            }
        }
    }

Terms of use Copyright © 2016-2019 Developer Express Inc.