delphi - 将 Windows 事件日志改造为 Delphi 5 应用程序

标签 delphi delphi-5 event-log

我正在寻找一种(相当轻松)的方法来向小型遗留 Delphi 5 应用程序添加一些 Windows 应用程序事件日志支持。我们只是希望它在启动、关闭、无法连接到数据库等时记录。

我见过的几个解决方案/组件似乎表明我们需要创建一个资源 DLL,Windows 事件日志查看器在尝试读取我们的“条目”时将链接到该资源 DLL。虽然这看起来并不太繁重,但我想如果/当我们将来进一步开发该应用程序时,还需要记住其他一些事情 - 我们需要使这个 DLL 保持最新。

在未来的某个时候,我们希望将应用程序转变为服务,可能是用 D2007 编写的。

那么有人可以推荐一个合适的路线来将事件添加到 D5 中的事件日志中吗?我正在寻找特定的“我们使用了这个,没关系”评论,而不是 Google 拖网(我可以自己做!)免费或付费,真的不介意 - 但有什么我将来可以迁移到 D2007 很重要。

最佳答案

摘要:使用 Delphi 写入 Windows 事件日志

<小时/>

如果您正在编写 Windows 服务并需要写入本地计算机的 Windows 事件日志,那么您可以调用 TService.LogMessage如前所述here

//TMyTestService = class(TService)

procedure TMyTestService.ServiceStart(Sender: TService; var Started: Boolean);
begin
  LogMessage('This is an error.');
  LogMessage('This is another error.', EVENTLOG_ERROR_TYPE);
  LogMessage('This is information.', EVENTLOG_INFORMATION_TYPE);
  LogMessage('This is a warning.', EVENTLOG_WARNING_TYPE);
end;
<小时/>

对于任何其他类型的应用程序,您可以使用 SvcMgr。TEventLogger undocumented如前所述,TService 的帮助程序类写入本地计算机的 Windows 事件日志 here , herehere .

uses
  SvcMgr;

procedure TForm1.EventLoggerExampleButtonClick(Sender: TObject);
begin
  with TEventLogger.Create('My Test App Name') do
  begin
    try
      LogMessage('This is an error.');
      LogMessage('This is another error.', EVENTLOG_ERROR_TYPE);
      LogMessage('This is information.', EVENTLOG_INFORMATION_TYPE);
      LogMessage('This is a warning.', EVENTLOG_WARNING_TYPE);
    finally
      Free;
    end;
  end;
end;
<小时/>

您还可以使用 Windows API ReportEvent功能如上所述herehere

我创建了一个简单的类以使其更容易,它是 available on GitHub .

//----------------- EXAMPLE USAGE: ---------------------------------

uses
  EventLog;

procedure TForm1.EventLogExampleButtonClick(Sender: TObject);
begin
  TEventLog.Source := 'My Test App Name';

  TEventLog.WriteError('This is an error.');
  TEventLog.WriteInfo('This is information.');
  TEventLog.WriteWarning('This is a warning.');
end;

//------------------------------------------------------------------


unit EventLog;

interface

type
  TEventLog = class
  private
    class procedure CheckEventLogHandle;
    class procedure Write(AEntryType: Word; AEventId: Cardinal; AMessage: string); static;
  public
    class var Source: string;
    class destructor Destroy;

    class procedure WriteInfo(AMessage: string); static;
    class procedure WriteWarning(AMessage: string); static;
    class procedure WriteError(AMessage: string); static;

    class procedure AddEventSourceToRegistry; static;
  end;

threadvar EventLogHandle: THandle;

implementation

uses Windows, Registry, SysUtils;

class destructor TEventLog.Destroy;
begin
  if EventLogHandle > 0 then
  begin
    DeregisterEventSource(EventLogHandle);
  end;
end;

class procedure TEventLog.WriteInfo(AMessage: string);
begin
  Write(EVENTLOG_INFORMATION_TYPE, 2, AMessage);
end;

class procedure TEventLog.WriteWarning(AMessage: string);
begin
  Write(EVENTLOG_WARNING_TYPE, 3, AMessage);
end;

class procedure TEventLog.WriteError(AMessage: string);
begin
  Write(EVENTLOG_ERROR_TYPE, 4, AMessage);
end;

class procedure TEventLog.CheckEventLogHandle;
begin
  if EventLogHandle = 0 then
  begin
   EventLogHandle := RegisterEventSource(nil, PChar(Source));
  end;
  if EventLogHandle <= 0 then
  begin
    raise Exception.Create('Could not obtain Event Log handle.');
  end;
end;

class procedure TEventLog.Write(AEntryType: Word; AEventId: Cardinal; AMessage: string);
begin
  CheckEventLogHandle;
  ReportEvent(EventLogHandle, AEntryType, 0, AEventId, nil, 1, 0, @AMessage, nil);
end;

// This requires admin rights. Typically called once-off during the application's installation
class procedure TEventLog.AddEventSourceToRegistry;
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKey('\SYSTEM\CurrentControlSet\Services\Eventlog\Application\' + Source, True) then
    begin
      reg.WriteString('EventMessageFile', ParamStr(0)); // The application exe's path
      reg.WriteInteger('TypesSupported', 7);
      reg.CloseKey;
    end
    else
    begin
      raise Exception.Create('Error updating the registry. This action requires administrative rights.');
    end;
  finally
    reg.Free;
  end;
end;

initialization

TEventLog.Source := 'My Application Name';

end.
<小时/>

ReportEvent支持将日志条目写入本地或远程计算机的事件日志。有关远程示例,请参阅 John Kaster's EDN article .

<小时/>

请注意,您还必须create a message fileregister your event source 否则你的所有日志消息都将以如下内容开头:

The description for Event ID xxx from source xxxx cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event:

1、有关如何创建消息文件的更多信息,请参阅Finn Tolderlund's tutorialMichael Hex's article 或者您可以使用现有的 MC 和 RES file included in the GitHub project .

2、通过将 MessageFile.res 包含在 DPR 文件中,将 RES 文件嵌入到您的应用程序中。或者,您可以为消息创建一个 dll。

program MyTestApp;

uses
  Forms,
  FormMain in 'FormMain.pas' {MainForm},
  EventLog in 'EventLog.pas';

{$R *.res}
{$R MessageFile\MessageFile.res}

begin
  Application.Initialize;

3,一次性注册需要管理员权限写入注册表,因此我们通常将其作为应用程序安装过程的一部分来完成。

//For example
AddEventSourceToRegistry('My Application Name', ParamStr(0));
//or
AddEventSourceToRegistry('My Application Name', 'C:\Program Files\MyApp\Messages.dll');

//--------------------------------------------------

procedure AddEventSourceToRegistry(ASource, AFilename: string);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKey('\SYSTEM\CurrentControlSet\Services\Eventlog\Application\' + ASource, True) then
    begin
      reg.WriteString('EventMessageFile', AFilename);
      reg.WriteInteger('TypesSupported', 7);
      reg.CloseKey;
    end
    else
    begin
      raise Exception.Create('Error updating the registry. This action requires administrative rights.');
    end;
  finally
    reg.Free;
  end;
end;
<小时/>

如果您需要 Windows 事件日志记录和其他日志记录要求,您还可以使用日志记录框架,例如 log4dTraceTool

<小时/>

参见here如果您想写入 Delphi IDE 中的“事件日志”窗口。

关于delphi - 将 Windows 事件日志改造为 Delphi 5 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/684919/

相关文章:

ios - 如何用 objective-c 类做IS?

.net-core - 在 NLog for .NET Core 应用程序中添加 EventLog 作为目标会导致解析配置错误

.net - .NET 是否支持 Windows Eventing 6.0?

delphi - EnumerateTraceGuids 返回 "The parameter is incorrect"(87)

string - Delphi 5中的OleVariant和String分配

delphi - 安装适用于 Windows 10 的 Delphi 5

c# - 为什么 WriteEntry 不适用于此 EventLog 源?

delphi - TDataSource组件放在哪里?

delphi - 如何在DWScript中公开记录的动态数组?

delphi - 使用 TRichEdit 的 TEdit 上下文菜单