Use logging in driver extension - COPA-DATA/DriverExtensions GitHub Wiki

For debugging and support purposes an adequate logging increases traceability and reduces the time of troubleshooting.

For that reason, the InitializeAsync-Method passes an argument to a ILogger interface that allows add log entries to the zenon logging system.

  private ILogger _logger;
  private IValueCallback _valueCallback;

  public Task InitializeAsync(ILogger logger, IValueCallback valueCallback, string configFilePath)
  {
    _logger = logger;
    _valueCallback = valueCallback;

    // Write Hello World to zenon Logging.
    _logger.Info("Hello World!");

    return Task.CompletedTask;
  }

By using Diagnosis Viewer, by enabling module "Driver" for GenericNet client, logging messages are shown. GenericNet driver itself create log messages if the driver extension cannot be loaded for whatever reason. Additionally, unhandled exceptions are logged as error too. The ILogger interface allows to create log messages with different levels. Please ensure that you use appropriate log levels:

  • DeepDebug - Only when I would be "tracing" the code and trying to find one part of a function specifically.
  • Debug - Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
  • Info - Generally useful information to log (service start/stop, configuration assumptions, etc). This should be the out-of-the-box config level.
  • Warn - Anything that can potentially cause application oddities, but for which automatically recovering applicable. (Such as switching from a primary server to a secondary, retrying an operation, missing secondary data, etc.)
  • Error - Any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved for incorrect connection strings, missing services, etc.

namespace CopaData.Drivers.Contracts
{
  public interface ILogger
  {
    void Error(string message);
    void Info(string message);
    void Warn(string message);
    void Debug(string message);
    void DeepDebug(string message);
  }
}