我们都知道sqlserver 管理工具 提供了Trace跟踪,可以跟踪程序执行了那些sql语句,非常的方便,今天我们自己用FireDac 来实现这个功能吧
要实现sql语句的跟踪,我们需要用到 TFDMoniFlatFileClientLink 控件,
示例
FDMoniFlatFileClientLink1.Tracing := True;with FDConnection1.Params do beginClear;
Add('DriverID=SQLite');Add('Database=c:\test.sdb');Add('MonitorBy=FlatFile');end;FDConnection1.Connected := True;...
// disable trace output for connection
FDConnection1.ConnectionIntf.Tracing := False;...
// enable trace output for connection
FDConnection1.ConnectionIntf.Tracing := True;
不多说,上源码
1、窗体文件 ( 在新建窗体上 右键 view as text 替换新建的窗体就ok)
object Form5: TForm5
Left = 0
Top = 0
Caption = 'FireDac SQL'#35821#21477#36319#36394
ClientHeight = 387
ClientWidth = 641
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object dbgrd1: TDBGrid
Left = -8
Top = 8
Width = 641
Height = 0
Align = alCustom
DataSource = ds
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
end
object btn1: TButton
Left = 558
Top = 54
Width = 75
Height = 25
Caption = #26597#35810
TabOrder = 1
OnClick = btn1Click
end
object mmoLog: TMemo
Left = 0
Top = 280
Width = 641
Height = 107
Align = alBottom
Lines.Strings = (
'mmoLog')
TabOrder = 2
ExplicitTop = 286
end
object dbgrd2: TDBGrid
Left = 0
Top = 110
Width = 641
Height = 170
Align = alBottom
DataSource = ds
TabOrder = 3
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
end
object dbnvgr1: TDBNavigator
Left = 0
Top = 85
Width = 641
Height = 25
DataSource = ds
Align = alBottom
TabOrder = 4
ExplicitLeft = 232
ExplicitTop = 48
ExplicitWidth = 240
end
object btn2: TButton
Left = 472
Top = 56
Width = 75
Height = 25
Caption = #36830#25509#25968#25454#24211
TabOrder = 5
OnClick = btn2Click
end
object FDCon: TFDConnection
Left = 144
Top = 144
end
object FDQuery: TFDQuery
Connection = FDCon
Left = 200
Top = 144
end
object GUIWaitCur: TFDGUIxWaitCursor
Provider = 'Forms'
Left = 504
Top = 96
end
object FDMonLink: TFDMoniFlatFileClientLink
OnOutput = FDMonLinkOutput
FileName = 'D:\trace2.txt'
FileAppend = True
FileColumns = [tiMsgText]
ShowTraces = False
Left = 496
Top = 144
end
object OraDL: TFDPhysOracleDriverLink
Left = 520
Top = 208
end
object ds: TDataSource
DataSet = FDQuery
Left = 248
Top = 144
end
end
unit umain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.VCLUI.Wait, FireDAC.Phys.OracleDef, Data.DB, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.DBCtrls, Vcl.Grids, Vcl.DBGrids, FireDAC.Phys.Oracle, FireDAC.Moni.Base, FireDAC.Moni.FlatFile, FireDAC.Comp.UI, FireDAC.Comp.DataSet, FireDAC.Comp.Client; type TForm5 = class(TForm) FDCon: TFDConnection; FDQuery: TFDQuery; GUIWaitCur: TFDGUIxWaitCursor; FDMonLink: TFDMoniFlatFileClientLink; OraDL: TFDPhysOracleDriverLink; dbgrd1: TDBGrid; ds: TDataSource; btn1: TButton; mmoLog: TMemo; dbgrd2: TDBGrid; dbnvgr1: TDBNavigator; btn2: TButton; procedure btn1Click(Sender: TObject); procedure FDMonLinkOutput(ASender: TFDMoniClientLinkBase; const AClassName, AObjName, AMessage: string); procedure btn2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form5: TForm5; implementation {$R *.dfm} procedure TForm5.btn1Click(Sender: TObject); begin FDQuery.Close; FDQuery.Open('select *From tabname where colname=''张三'''); end; procedure TForm5.btn2Click(Sender: TObject); begin FDMonLink.FileName := ExtractFilePath(Application.ExeName) + '\sqllog.txt'; //文件保存的路径 FDMonLink.FileAppend := true; //文件是否已追加的方式存储 //FileEncoding;//文件保存格式,默认 ANSI //FileColumns://文件保存那些列 【tiRefNo, tiTime, tiThreadID, tiClassName,tiObjID, tiMsgText】 FDMonLink.EventKinds := [ekCmdExecute]; // EventKinds 过滤输出,指定输出那些类型 {(ekLiveCycle, ekError, ekConnConnect, ekConnTransact, ekConnService, ekCmdPrepare, ekCmdExecute, ekCmdDataIn, ekCmdDataOut, ekAdaptUpdate,ekVendor,ekComponent)} FDMonLink.Tracing := True; with FDCon.Params do //连接参数 begin Clear; Add('DriverID=Ora'); Add('Database=localhost'); Add('User_Name=ERP'); Add('Password=ERP123'); Add('MonitorBy=FlatFile');//这个必须加 end; FDCon.Connected := True; //连接数据库 // 关闭sql日志记录 FDCon.ConnectionIntf.Tracing := False; // 开启日志记录 FDCon.ConnectionIntf.Tracing := True; end; procedure TForm5.FDMonLinkOutput(ASender: TFDMoniClientLinkBase; const AClassName, AObjName, AMessage: string); begin mmoLog.Lines.Add(AClassName + '----' + AObjName + '-----' + AMessage);
//不设置 TFDMoniFlatFileClientLink的 FileName,不让控件自动输出日志,自己写一个写日志的方法 通过pos AClassName, AObjName的值 这里可以进一步过滤输出end;end.
群:Delphi Home 235236282,欢迎Delphi爱好者加入,共同学习进步。