FireDAC.MoniLayerClients Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates how to use different FireDAC monitor clients such as FlatFile client or Remote client.

Location

You can find the MoniLayerClients sample project at:

  • Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
    • Object Pascal\Database\FireDAC\Samples\Moni Layer\Clients
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

The MoniLayerClients sample shows you how to use the FireDAC tracing and monitoring capabilities to show how an application is communicating with a FireDAC monitor client. To this end, the sample uses two different FireDAC monitor clients: FlatFile-client and Remote-client. To use both monitor clients, the sample uses two client link components: the TFDMoniFlatFileClientLink component and the TFDMoniRemoteClientLink component.

How to Use the Sample

  1. Navigate to the location given above and open Clients.dproj.
  2. Press F9 or choose Run > Run.
  3. Click on the Use Connection Definition combo box and select an option; for example: EMPLOYEE.
  4. Select a monitor client.

Files

File in Delphi Contains

Clients.dproj
Clients.dpr

The project itself.

fClients.pas
fClients.fmx

The main form.

Implementation

The following steps are needed to implement the sample.

Define the trace / monitor output of a connection definition

Type the following code to set up a connection definition using none, remote or flat file monitoring.

var
  oConnectionDef: IFDStanConnectionDef;
begin
  oConnectionDef := ...
  ...
  // Set up connection def to use none client
  oConnectionDef.Params.MonitorBy := 'mbNone';
  ...

or

  ...
  // Set up connection def to use Remote client
  oConnectionDef.MonitorBy := 'mbRemote'
  ...

or

  ...
  // Set up connection def to use Remote client
  oConnectionDef.Params.MonitorBy := 'mbFlatFile';
  ...

Enable / Disable the tracing

To enable the tracing, set the Tracing property of the component to True. To disable the tracing, set the Tracing property of the component to False.

FDMoniRemoteClientLink1: TFDMoniRemoteClientLink;
FDMoniFlatFileClientLink1: TFDMoniFlatFileClientLink;
// ...
// ...
FDMoniRemoteClientLink1.Tracing := True;
FDmoniFlatFileClientLink1.Tracing := True;
// ...

or

// ...
FDMoniRemoteClientLink1.Tracing := False;
FDMoniFlatFileClientLink1.Tracing := False;
//...

Controlling the output

To control the output of each client link component, use the Object Inspector to make some settings in design time.

The TFDMoniRemoteClientLink component controls the remote trace output. It outputs the trace to the FDMonitor utility and allows to monitor the application. FDMonitor must be running before activating a trace output. To control the output, set up the following properties:

...
// Use the Host property to specify the FDMonitor host IP addres
FDMoniRemoteClientLink1.Host := '127.0.0.1';
// Use the Port property to specify the IP port where FDMonitor is listening
ADMoniRemoteClientLink1.Port := 8050;
// The EventKinds property specifies the information set to send to FDMonitor
ADMoniRemoteClientLink1.EventKinds := [ekLiveCycle (...) ekVendor];
...

The TFDMoniFlatFileClientLink component outputs the trace to a flat text file. When the application finishes, it shows the list of produced trace files. By default, the trace file name is automatically generated in the Temp folder of your system. To control the output, set up the following properties:

...
// You may specify trace file name here
FDMoniFlatFileClientLink1.FileName := 'C:\Users\test\AppData\Local\Temp\trace.txt';
// Set up whether append or not the records to your file
FDMoniFlatFileClientLink1.FileAppend := False;
...

Output user messages

Use this code to produce custom trace outputs.

var
  oMoni: IFDMoniClient;
begin
  //...
  oMoni.Tracing := True;
  oMoni.Notify(ekVendor, esStart,    Self, 'Start monitoring',        ['Form', 'frmClients']);
  oMoni.Notify(ekVendor, esProgress, Self, 'Progress monitoring',     ['Form', 'frmClients']);
  oMoni.Notify(ekVendor, esEnd,      Self, 'End monitoring',          ['Form', 'frmClients']);
  oMoni.Notify(ekError,  esProgress, Self, 'Error during monitoring', ['Form', 'frmClients']);
  oMoni.Tracing := False;
  //...

Output example

The following image is an example of the trace output produced with a flat file monitor client.

FlatFileTrace.png

See Also