我的实现方法:
1.定义一个全局类:
{ TGlobal 全局变量 }
TGlobal = class(TBaseItem) //TBaseItem 可以改为 TObject
private
FWriteLogSwitch: Boolean;
public
property WriteLogSwitch: Boolean read GetWriteLogSwitch; //日志开关: 0, 不写; 1, 写入
procedure WriteEventLog(OperatorId: Integer; Content: string); //写入事件日志,受 WriteLogSwitch 值影响
end;
implementation
procedure TGlobal.WriteEventLog(OperatorId: Integer; Content: string);
begin
if WriteLogSwitch = False then Exit;
with DataModule_Globe.CDS_Query_ExecSQL do
begin
Close;
CommandText := 'EXEC proc_insert_EventLog :@OperatorId,:
@Content ';
Params.ParamByName('@OperatorId').AsInteger := OperatorId;
Params.ParamByName('@Content').AsString := Content;
Execute;
Close;
end;
end;
end.
2.调用方法:(GlobalValue 为全局类的实体对象 )
在需要的地方,写以下语句:
GlobalValue.WriteEventLog(Operator.ID, '工作流[WorkflowID: ' + IntToStr(mTmpWorkflow.ID) + '] 保存成功');
3.数据库中的事件日志表及存储过程:
if exists (select 1 from sysobjects where id = object_id('EventLog') and type = 'U') drop table EventLoggo
/*==============================================================*//* Table: EventLog *//*==============================================================*/create table EventLog ( ID integer identity, EventTime datetime null, OperatorId integer not null default -1, Content text null, constraint PK_EVENTLOG primary key nonclustered (ID))go
if exists (select 1 from sysobjects where id = object_id('proc_insert_EventLog') and type = 'P') drop procedure proc_insert_EventLoggo
create procedure proc_insert_EventLog( @OperatorId integer, @Content text) asbegin insert into EventLog(EventTime,OperatorId,Content) values(getdate(),@OperatorId,@Content)endgo
I