Главная » Delphi » Основная » Как писать в EventLog?

Как писать в EventLog?

19:41
http://www.delphimaster.ru/cgi-bin/forum.pl?id=1139299249&n=5
Lucefer (07.02.06 11:00)
Пишу сервис, соотвтетственно никаких окон об ошибках быть не должно, все перехватываю по exception. Сейчас кидаю сообщения в текстовый файл, а как их записать в журнал событий?
----------------------------------
Игорь Шевченко © (07.02.06 11:40) [1]
RegisterEventSource,ReportEvent

unit eventlog;
 
interface
 
uses
 
Windows, SysUtils;
 
var
 
hEventLog: THandle;
 
procedure LOGWRITE(EventCat: Word; EventType: UINT; EventId: DWORD; const Msg: string); overload;
procedure LOGWRITE(EventCat: Word; EventType: UINT; EventId: DWORD; const Msg: string; Args: array of const); overload;
procedure LOGMESSAGE(const Msg: string); overload;
procedure LOGMESSAGE(const Msg: string; Args: array of const); overload;
procedure LOGWARNING(const Msg: string); overload;
procedure LOGWARNING(const Msg: string; Args: array of const); overload;
procedure LOGERROR(const Msg: string); overload;
procedure LOGERROR(const Msg: string; Args: array of const); overload;
 
function OpenLog(const EventSource: string): Boolean;
procedure SetLogCategory(EventCategory: Word);
procedure CloseLog;
 
implementation
 
const
 
EVT_MY_ERROR = $C0000001;
EVT_MY_WARNING = $80000002;
EVT_MY_INFO = $40000003;
 
var
 
EventCat: Word = 0;
 
function OpenLog(const EventSource: string): Boolean;
begin
if hEventLog = 0 then
hEventLog := RegisterEventSource(nil, PChar(EventSource));
Result := hEventLog <> 0;
end;
 
procedure CloseLog;
begin
if hEventLog <> 0 then begin
DeregisterEventSource(hEventLog);
hEventLog := 0;
end;
end;
 
procedure SetLogCategory(EventCategory: Word);
begin
EventCat := EventCategory;
end;
 
procedure LOGWRITE(EventCat: Word; EventType: UINT; EventId: DWORD; const Msg: string);
var
P: PChar;
begin
P := PChar(Msg);
ReportEvent(hEventLog, EventType, EventCat, EventId, nil, 1, 0, @P, nil);
end;
 
procedure LOGWRITE(EventCat: Word; EventType: UINT; EventId: DWORD; const Msg: string; Args: array of const);
var
S: string;
P: PChar;
begin
S := Format(Msg, Args);
P := PChar(S);
ReportEvent(hEventLog, EventType, EventCat, EventId, nil, 1, 0, @P, nil);
end;
 
procedure LOGMESSAGE(const Msg: string);
begin
LOGWRITE(EventCat, EVENTLOG_INFORMATION_TYPE, EVT_MY_INFO, Msg);
end;
 
procedure LOGMESSAGE(const Msg: string; Args: array of const);
begin
LOGWRITE(EventCat, EVENTLOG_INFORMATION_TYPE, EVT_MY_INFO, Msg, Args);
end;
 
procedure LOGWARNING(const Msg: string);
begin
LOGWRITE(EventCat, EVENTLOG_WARNING_TYPE, EVT_MY_WARNING, Msg);
end;
 
procedure LOGWARNING(const Msg: string; Args: array of const);
begin
LOGWRITE(EventCat, EVENTLOG_WARNING_TYPE, EVT_MY_WARNING, Msg, Args);
end;
 
procedure LOGERROR(const Msg: string);
begin
LOGWRITE(EventCat, EVENTLOG_ERROR_TYPE, EVT_MY_ERROR, Msg);
end;
 
procedure LOGERROR(const Msg: string; Args: array of const);
begin
LOGWRITE(EventCat, EVENTLOG_ERROR_TYPE, EVT_MY_ERROR, Msg, Args);
end;
 
initialization
hEventLog := 0;
finalization
CloseLog;
end.

http://www.delphimaster.ru/cgi-bin/forum.pl?id=1139299249&n=5