How to take logs while we develop Extension

Sufficient logging allows answering nearly any question about what a program is doing. There are two sides we see to increased visibility: the first is visibility into what our code is doing after it’s running and people are using the software we have written. The second is visibility into your code during development. There are different ways to take logs depending on the framework we are talking about. On this page, we shall describe how we can take logs for the Beckhoff HMI extension module.

TcHmiSrv is a namespace where we have Context. ITcHmiSrvExtHost interface has a method send. This can be used to write log file to the HomeAutomationMainDoorStatusReader\.engineering_servers\HomeAutomationMainDoorStatusReader as SQlite database. HomeAutomationMainDoorStatusReader is our project name.

 All extension developed with C#  is derived from the interface IExtension. When the server starts then it initializes the extension by calling the following method on the interface. We save the host and context to use for the extension and creating a log.
 
namespace TcHmiSrv.Management
{
    public interface IExtension
    {
        ErrorValue Init(ITcHmiSrvExtHost host, Context context);
    }
}

We need to hold the host and context and call send method to record in SQLite database.

         // Sends a message with the specified context, name, parameters and severity.
        public void Send(Context context, string name, IEnumerable<string> parameters = null, Severity severity = Severity.Severity_Info)
        {
            Message message = new Message(context, severity, name);

            if (parameters != null)
            {
                foreach (string parameter in parameters)
                {
                    message.Parameters.insert(message.Parameters.Size.ToString(), parameter);
                }
            }

            _host.send(context, message);
        }

This is an example when the init method is called we can record an appropriate message to the database.

_logger.Send(context, "MESSAGE_INIT Called OK");

The logger database file can be opened by the SQLite browser. Here are the screenshots for our home automation system step by step.

Finally we browse the database and see that our text is there.

See Also:  How to use log4Net to debug extension in C# https://www.hemelix.com/software/log4net-for-hmi-extension/