Extending and Customizing the SmartInspect Libraries

Introduction
Extending the Functionality
Customizing the Logging Behavior
Example of Using the Event System
Conclusion

Introduction

The SmartInspect libraries provide several ways to extend and customize their functionality. It is easily possible to send custom data which can be displayed in the exact same powerful manner like the other data. Furthermore the libraries provide several events which are very useful to customize the logging behavior.

This article begins with the description of how to log custom data. A detailed example is used to demonstrate this powerful feature. The article then proceeds with the explanation of the event system in the libraries. Finally it concludes with an example of how to apply the tips about the events.

Extending the Functionality

To extend the functionality of the SmartInspect libraries by logging custom data you need to choose an appropriate viewer in the Console which should display your data. If you want to display your data as a table then you can choose the TableViewer, if your data represents a list then you can choose the ListViewer and so on. Every viewer in the Console has a corresponding so called viewer context class in the libraries. A viewer context is capable of processing data and to format them in a way so that the related viewer in the Console can handle it.

The following example is written in C#, but the Delphi and Java libraries provide the same functionality. This example uses the InspectorViewer in the Console to display the custom data. The InspectorViewer is capable of displaying key/value pairs separated into multiple groups like the well-known object inspectors of popular IDEs. As you can see you just need to create an instance of the related InspectorViewerContext class and fill it with your custom data. Once filled, you then can display the custom data in the Console by logging the viewer context with the LogCustomContext method of the Session class.

using System;
using Gurock.SmartInspect;

public class CustomExample
{
    public static void Main(string[] args)
    {
        // Enable SmartInspect logging.
        SiAuto.Si.Enabled = true;

        // Create a custom inspector viewer context.
        InspectorViewerContext ctx = new InspectorViewerContext();
        try
        {
            // Add a group and the related entries.
            ctx.StartGroup("FindOptions");
            ctx.AppendKeyValue("Regex", true);
            ctx.AppendKeyValue("WholeWord", false);
            ctx.AppendKeyValue("CaseSensitive", true);
            ctx.AppendKeyValue("Title", "Foobar");

            // Start another group.
            ctx.StartGroup("Tcp");
            ctx.AppendKeyValue("ListenOnStartup", true);
            ctx.AppendKeyValue("Port", 4228);
            ctx.AppendKeyValue("UseAllInterfaces", true);

            // Then send the custom context.
            SiAuto.Main.LogCustomContext(
                "Custom Data", LogEntryType.Text, ctx
            );
        }
        finally 
        {
            ctx.Dispose();
        }
    }
}

The following screenshot shows the InspectorViewer in the Console while displaying the custom data of the example above. As you can see the custom data is divided into two different groups which contain several entries. Besides the InspectorViewer you can choose from several other viewers to display custom data including viewers which can display key/value lists, tables, simple text or even pictures. For a list of available viewers in the Console please have a look at the SmartInspect product description or the SmartInspect screenshots.

The InspectorViewer displaying the example custom data The InspectorViewer displaying the example custom data

Customizing the Logging Behavior

The libraries provide several events which can be divided into two groups. On the one hand there is the error event which is used for error reporting and on the other hand there are the packet events which are useful to customize the logging behavior. This article will explain the latter group.

The SmartInspect concept uses different packet types for different tasks. The main task is the sending of normal log messages, of course. But there are also packet types which handle variable watches, generate illustrated process and thread information or control the behavior of the SmartInspect Console. Every packet type has an event in the libraries which is fired after a corresponding packet has been sent.

To customize the behavior of the SmartInspect libraries now, you can register event handlers to one or more of these events. In the event handlers you can then write the properties of the packets to a database or display them in your own GUI application, for example. Please see the next chapter for a detailed example of how to apply these tips.

Example of Using the Event System

This example uses the LogEntry packet type and the corresponding event to demonstrate the event system. The LogEntry packet type is used for almost all logging methods, like, for example, LogMessage, LogObject or LogSql. This example is written in C#, but the Delphi and Java libraries provide the same functionality. As you can see, this example registers an event handler to the LogEntry event. This event handler is called after the LogEntry packet has been sent by the LogMessage method of the Session class. In this event handler you are now able to process the log message in your own way.

using System;
using Gurock.SmartInspect;

public class EventExample
{
    private static void EventHandler(object sender,
        LogEntryEventArgs args)
    {
        LogEntry logEntry = args.LogEntry;

        // In this event handler you are now able to process the
        // LogEntry in your own way. You can, for example, write
        // its properties to a database or could display them in
        // your own GUI application.

        System.Console.WriteLine(logEntry.Title);
    }

    public static void Main(string[] args)
    {
        SiAuto.Si.Enabled = true; // Enable SmartInspect logging
        SiAuto.Si.LogEntry += new LogEntryEventHandler(EventHandler);
        SiAuto.Main.LogMessage("This is an event test!");
    }
}

Conclusion

After reading this article you should now have an overview over how to log custom data and how to use the different events to customize the logging behavior of the SmartInspect libraries. If you have any further questions about this topic do not hesitate to contact me at tg@gurock.com.

Try SmartInspect

Get started in minutes and try SmartInspect free for 30 days.

Try SmartInspect