在application.OnException中将错误信息写到文件里呀procedure TForm1.ApplicationEvents1Exception(Sender: TObject;
  E: Exception);
const
  cr: string = #13#10;
var
  f: Integer;
begin
  f := fileopen('c:\debug.txt', fmOpenWrite);
  fileseek(f, 0, 2);
  filewrite(f, e.message[1], length(e.message));
  filewrite(f, cr[1], 2);
  fileclose(f);
end;另外在Form.OnCreate中新建Debug.txt就等于清空了

解决方案 »

  1.   

    建议你写道系统日志中去
    这里有一个例子
    ////////////////////////////////////////////////////////////////////////////
    //EventLog.dll                                                            //
    //Writed by zhjh                                                          //
    //Date:2002.4                                                             //
    //function LogEvent(ApplicationName,FUserName,EventType:pchar;            //
    //                 IncludeUserName:word;EventCategory:word;EventID: DWord;//
    //                 const Line: pchar) :word; Export;                      //
    //参数说明:                                                              //
    //ApplicationName: 应用程序名称                                           //
    //      EventType: 事件类型                                               //
    //      etError:错误                                                     //
    //      etWarning:警告                                                   //
    //      etInformation:信息                                               //
    //      etAuditSuccess:成功审核                                          //
    //      etAuditFailure):失败审核                                         //
    //IncludeUserName: 是否包括操作系统登陆用户名 1:包括;0:不包括           //
    //EventCategory:事件种类                                                 //
    //EventID:事件ID                                                         //
    //Line:写入日志的具体附加信息                                             //
    //正确返回非零,否则返回0                                                 //
    //注:请参照windows的事件查看器                                           //
    ////////////////////////////////////////////////////////////////////////////
    unit LogEventDLL;interface
    uses
      Windows, Registry, SysUtils;function LogEvent(ApplicationName,FUserName,EventType:pchar;
                      IncludeUserName:word;EventCategory:word;EventID: DWord;
                      const Line: pchar) :word; Export;stdcall;implementationfunction LogEvent(ApplicationName,FUserName,EventType:pchar;
                      IncludeUserName:Word;EventCategory:word;EventID: DWord;
                      const Line: pchar) :word;
    const
      cRegPath = '\SYSTEM\CurrentControlSet\Services\EventLog\Application';
    var
      LogHandle: THandle;
      OK,OK2Run: Boolean;
      eType: Word;
      eMsg, aName: PChar;
      Reg: TRegistry;
      nSize: Dword;
      VersionInfo : TOSVersionInfo;
      sUserName,sLine:string;
    begin
      VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
      Ok2Run := False;
      if Windows.GetVersionEx( VersionInfo ) then
        if VersionInfo.dwPlatformId >= VER_PLATFORM_WIN32_NT then
          Ok2Run := True;
        if Ok2Run then
        begin
          Reg := TRegistry.Create;
          try
          with Reg do
          begin
            RootKey := HKEY_LOCAL_MACHINE;
            OpenKey( cRegPath + '\' + strpas(ApplicationName), True );
            CloseKey;
          end;
          finally
          Reg.Free;
        end;    LogHandle:= OpenEventLog( NIL, PChar(ApplicationName) );
        if Loghandle<>0 then
        begin
          eType := 0;
          if EventType= 'etError' then  eType := 1;
          if EventType= 'etWarning'then eType := 2;
          if EventType= 'etInformation'then eType := 4;
          if EventType= 'etAuditSuccess'then eType := 8;
          if EventType= 'etAuditFailure'then eType := 16;      FUsername := #13#10;
          If IncludeUserName=1 then
          begin
            nSize := 20 ; // Max UserName
            aName := stralloc ( nSize+1 );
            OK := GetUserName( aName, nSize );
            if not OK then strcopy( aName, 'N/A' );
            sUserName := strpas(FUserName) + 'User: ' + aName + #13#10;
            strDispose( aName );
          end;
          sLine:=strpas(Line);
          eMsg := Pchar(sUserName + sLine);
          if ReportEvent(LogHandle, eType, EventCategory, EventID, NIL, 1, 0, @eMsg, NIL) then
            result:=1
          else
            result:=0;
          CloseEventLog(LogHandle);
        end;
      end;
    end;
    end.
      

  2.   

    各位老大,好象最简单的方法就是在打开文件的时候用rewrite打开,这样每次都会从新写入。