Component Ultra-Fast Way To Retrieve Event Logs In Your Delphi App With Robust Component Suite

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Ultra-Fast Way To Retrieve Event Logs In Your Delphi App With Robust Component Suite
By Anbarasan March 23, 2021

Developers may often need to retrieve the windows event messages to diagnose system problems and predict future issues. How to retrieve event logs programmatically for the given source such as System, Security, Hardware events, etc. ? Don’t know how to do? Don’t worry? Для просмотра ссылки Войди или Зарегистрируйся System Information Management Suite’s component helps to retrieve event messages quickly and with less code. we will learn how to use use the TMiTec_EventLog, component in this blog post.

Platforms: Windows.

Installation Steps:

You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.
  1. Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
  2. Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
  3. It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:
  • Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi12
  • Open the ELView project in RAD studio 10.4.1 Compile and Run the application.
  • This Demo App shows how to retrieve event logs programmatically for the given source and details of the particular event message.
Components used in MSIC ELView Demo App:
1616570835202.png

  • TMiTeC_EventLog: Retrieves Windows Event Log messages for given source.
  • TComboBox to list the Event Source category such as System, Security, Hardware events.
  • TEdit to provide the filter text which helps to filter user preferred the event log messages
  • TListView to list the event log messages for a particular source.
  • TButton’s to save and refresh.
Implementation Details:

  • An instance is created EL of TMiTeC_EventLog, and source event source containers is retrieved by looping the ContainerCount property. Use OnReadEventLog to update the application message caption for each 1000 event messages.
  • SourceFilter property helps filter the text within the event log messages. Set this property with TEdit Text value.
  • On changing the combo box, list the event logs by looping the RecordCount, For each record of TLogRecord type provides the EventType, DateTime, Source, Category, EventID, Username, Domain, Computer, Description, BinaryData, CharData values.
  • You can provide the Username, Password, DomainName for connecting to remote machine for the new WinEvt API.
Код:
procedure TForm1.cbChange(Sender: TObject);
var
  i: Integer;
  h: Boolean;
begin
  Memo.Lines.Clear;
  if cb.ItemIndex=-1 then
    Exit;
 
  bAction.Caption:='Cancel';
  bAction.OnClick:=cmCancel;
  bLoad.Enabled:=False;
  bSave.Enabled:=False;
  cb.Enabled:=False;
  eFilter.Enabled:=False;
  lv.Enabled:=False;
  Memo.Enabled:=False;
  FCancel:=False;
  Screen.Cursor:=crHourglass;
  try
    et:=GetTickCount64;
    EL.SourceFilter:=eFilter.Text;
    EL.SourceName:=cb.Text;
    h:=True;
    FCancel:=False;
    Caption:=Format('EventLog Viewer - %d records / %1.2f s',[EL.RecordCount,(GetTickCount64-et)/1000]);
 
    with lv.Items do begin
      BeginUpdate;
      try
        Clear;
        Update;
        for i:=0 to EL.RecordCount-1 do
          with Add do begin
            Caption:=DatetimeToStr(EL.Records[i].DateTime);
            SubItems.Add(EL.Records[i].Source);
            SubItems.Add(IntToStr(EL.Records[i].EventID));
            SubItems.Add(EL.Records[i].Category);
            SubItems.Add(EL.Records[i].Computer);
            SubItems.Add(EL.Records[i].Description);
            ImageIndex:=Integer(EL.Records[i].EventType);
          end;
      finally
        EndUpdate;
      end;
    end;
  finally
    EL.Clear;
    bAction.Caption:='Refresh';
    bAction.OnClick:=cmRefresh;
    bLoad.Enabled:=True;
    bSave.Enabled:=True;
    cb.Enabled:=True;
    eFilter.Enabled:=True;
    lv.Enabled:=True;
    Memo.Enabled:=True;
    Screen.Cursor:=crDefault;
  end;
  lv.SetFocus;
end;
  • Show the selected items subitem in the Memo text.
Код:
procedure TForm1.lvSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
begin
  Memo.Lines.Text:=Item.SubItems[Item.SubItems.Count-1];
end;
1616570926155.png
MiTeC EventLog Demo

It’s really that simple to retrieve event logs and its event log message details from various event source in your application. Use this MiTeC component suite and get the job done quickly.