想在程序中生成LOG文件!(50分)

  • 主题发起人 主题发起人 divid_cui
  • 开始时间 开始时间
D

divid_cui

Unregistered / Unconfirmed
GUEST, unregistred user!
各位好!我想在我的程序中生成能记录错误内容的LOG文件,也可以是自己订制内容,不知道可不可以!请各位大虾指教!
 
可以,例如:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure FormActivate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; procedure ActionLog(showtime:bool;msg:string);<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure Tform1.ActionLog(showtime:bool;msg:string);<br>var<br>filename:string;<br>logfile:Textfile;<br>begin<br>filename:=changefileext(application.ExeName,'.log');<br>assignfile(logfile,filename);<br>if FileExists(filename) then<br>append(logfile)<br>else<br>rewrite(logfile);<br>if(showtime) then<br>writeln(logfile,datetimetostr(now)+':'+msg)<br>else<br>writeln(logfile,msg);<br>closefile(logfile);<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>ActionLog(true,Tbutton(sender).Name+'('+Tbutton(sender).Caption+')'+' Click!');<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>ActionLog(true,'Form Closed!');<br>ActionLog(false,'--------------------------------------------------------------')<br>end;<br><br>procedure TForm1.FormActivate(Sender: TObject);<br>begin<br>ActionLog(true,'Form Activate!');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>ActionLog(true,'Form Create!');<br>end;<br><br>end.<br>
 
谢谢!可否告知,文件的类型及名称相关内容<br>文件名是不是可以用字符串来表示?
 
日志文件名是:程序名.log<br>用notebook就可打开看<br>文件名当然可以用字符串表示。<br>
 
不好意思!是我没说明白!我在D6中没有发现有关于“TEXTFILE“之类的数据类型啊!如TEXTFILE;INIFILE之类的东西能到那里找啊!<br>另外在此程序代码中的ASSIGNFILE到底起的是什么作用啊?<br>WRITELN所需要的文件名是什么样的呢???<br>还烦您指导!<br>不胜感激!
 
拿一个Memo或者ListBox记录日志信息,让Memo或ListBox的Visible属性为False;<br>保存的时候用Memo.Lines.SaveToFile就可以了.
 
不胜感激!
 
不胜感激!
 
后退
顶部