Appendix A: Code Example of Ucommerce Connector as a Custom Console Application

Below you will find a C# code example of a console application, which uses Perfion Ucommerce Connector manager library with a custom logger and which uses most of the features the manager offers. The code can be adjusted and applied for other type applications.

All libraries one needs to use in any project which uses Connector manager can be found in Connector installation folder and directly linked to the project.

The Connector manager implements “IConnectorManager” interface as shown below. It allows subscribing to four events, which will be triggered at any important Connector manager related actions. The manager can also be started and stopped, and finally, one can get manager’s current status.

Console application – Perfion Connector IConnectorManager interface

public interface IConnectorManager {   event EventHandler<ConnectorStateEventArgs> StateChangeEvent;   event EventHandler<ConnectorErrorStateEventArgs> ErrorStateChangeEvent;   event EventHandler<ConnectorSyncTypeEventArgs> SyncTypeChangeEvent;   event EventHandler<ConnectorSyncProgressEventArgs> SyncProgressChangeEvent;    void Start( bool runOnce = false );   void Stop();   ConnectorStatus GetStatus(); }

The Connector manager can also use a custom logger. The logger must implement Perfion “ILogger” interface.

Console application – Perfion Connector ILogger interface

public enum LogType { Error, Warning, Progress, Info, Debug }  public interface ILogger {   LogType LogLevel { get; set; }    void Log( string message, LogType logType );   void LogDebug( string message );   void LogInfo( string message );   void LogProgress( string message );   void LogWarning( string message );   void LogError( string message );   void LogException( string message, Exception ex ); }

The example console application will use a custom logger “ExampleLogger”, which allows to intercept all log messages and then sends them via event. The “ExampleLogger” is initialized by specifying the maximum log message type it will accept and then it will log only those messages, which match this predefined level. For example, if “Progress” level is selected as a log level, then the logger will log only messages of type “Error”, “Warning” and “Progress”. Setting “Debug” as log level will allow to log messages of all types.

The “ExampleLogger” is shown only as an example, but it can be quickly modified to write log messages to the file, database, etc.

Console application – ExampleLogger example

using PerfionLogger; using System;  namespace UCommerceConsoleAppExample {   public class LoggerEventArgs : EventArgs {     DateTime date;     public DateTime Date { get { return date; } }      LogType logLevel;     public LogType LogLevel { get { return logLevel; } }      string message;     public string Message { get { return message; } }        public LoggerEventArgs( LogType logType, string message ) {       date = DateTime.Now;       logLevel = logType;       this.message = message;     }   }    public class ExampleLogger : ILogger {     static readonly object lockObj = new object();      public event EventHandler<LoggerEventArgs> LogEvent;      LogType logLevel;     public LogType LogLevel { get { return logLevel; } set { logLevel = value; } }      public ExampleLogger( LogType logType ) {       logLevel = logType;     }      public ExampleLogger( string logType ) {       if( !Enum.TryParse( logType, true, out logLevel ) ) {         logLevel = LogType.Progress;       }     }      public void LogDebug( string message ) {       Log( message, LogType.Debug );     }      public void LogInfo( string message ) {       Log( message, LogType.Info );     }      public void LogProgress( string message ) {       Log( message, LogType.Progress );     }      public void LogWarning( string message ) {       Log( message, LogType.Warning );     }      public void LogError( string message ) {       Log( message, LogType.Error );     }      public void LogException( string message, Exception ex ) {       message = $"{message}. Error: {ex}";       Log( message, LogType.Error );     }      public void Log( string message, LogType logType ) {       try {         lock( lockObj ) {           if( logType > logLevel ) return; // Control which messages are logged by using logLevel              LoggerEventArgs args = new LoggerEventArgs( logType, message );           LogEvent?.Invoke( null, args );         }       } catch { }     }   } }

The example console application does the following:

  • Reads parameters from configuration file

  • Initializes a custom “ExampleLogger” logger and subscribes to its event “LogEvent” for printing log messages to the console.

  • Initializes Connector manager. Note, the manager uses three loggers, but in this example we use only one logger. The manager consist of three interfaces: Perfion ECommerce API, Ucommerce API and manager itself, which uses those first two together to get everything in sync. These three interfaces may each have their own logger, but one can also use the same single logger for all interfaces.

  • Subscribes to all manager’s events to print all actions performed by manager to the console in real time. All the methods used to handle manager’s events will further show how one can access various types of manager state information.

  • Starts the manager in a new background thread using user’s chosen mode.

  • Starts the manager monitoring process, which every few seconds retrieves information from manager and in case it is in sleep mode, present how long it will take for manager to wake up. In case the manager is stopped, the console application will be terminated.

Console application – Main method

The console application uses configuration file to supply Connector manager with needed parameters. Below you can see an example of such configuration file. Note, configuration file is also used by Ucommerce API.

Console application – configuration file example