Options

Logging to textfile

lawlaw Member Posts: 19
Hi

I have to log some events to a text file with C/AL.
I write this proc:
GetKIRSetup;
IF EXISTS(KIRSetup."Log Filename") THEN
  LogFile.OPEN(KIRSetup."Log Filename")
ELSE
  LogFile.CREATE(KIRSetup."Log Filename");
LogFile.CREATEOUTSTREAM(LogStream);
LogStream.WRITETEXT(FORMAT(0D)+Szoveg);
LogFile.CLOSE;

LogFile and LogStream are local variables.
When the file doesn't exists, this procedure creates it, but if it exists, Navision gives me an error (access denied).
File attributes, permissions are ok.
What am I doing wrong? (or how to append to text file?)

Comments

  • Options
    kinekine Member Posts: 12,562
    If you want to overwrite this file do:
    GetKIRSetup;
    IF EXISTS(KIRSetup."Log Filename") THEN
      ERASE(KIRSetup."Log Filename");   //erase old file
    
    
    LogFile.CREATE(KIRSetup."Log Filename");
    LogFile.CREATEOUTSTREAM(LogStream);
    LogStream.WRITETEXT(FORMAT(0D)+Szoveg);
    LogFile.CLOSE;
    

    If you want append:
    GetKIRSetup;
    IF EXISTS(KIRSetup."Log Filename") THEN BEGIN
      LogFile.OPEN(KIRSetup."Log Filename");
      LogFile.SEEK(LogFile.LEN);  //seek to the end of file to append
    END ELSE 
      LogFile.CREATE(KIRSetup."Log Filename");
    end
    LogFile.CREATEOUTSTREAM(LogStream);
    LogStream.WRITETEXT(FORMAT(0D)+Szoveg);
    LogFile.CLOSE;
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    pduckpduck Member Posts: 147
    have a look here: http://www.navision.net/forum/topic.asp ... ppend,file

    maybe it is is easier to use a single instance codeunit instead of reopening the file ... but therefor you need navi 3.7 !
  • Options
    kvbkvb Member Posts: 107
    Hi,
    WRITEMODE (File)
    Use this function before you use OPEN (File) to set or test whether you can write to a file in later calls.

    Your error appeared because you had to set
    LogFile.WRITEMODE := TRUE;
    

    before you open file.

    Furthermore, try this code(without OutStream object type):
    LogFile.TEXTMODE := TRUE;
    LogFile.WRITEMODE := TRUE;
    
    IF EXISTS(KIRSetup."Log Filename") THEN
      LogFile.OPEN(KIRSetup."Log Filename")
    ELSE 
      LogFile.CREATE(KIRSetup."Log Filename");
    
    LogFile.SEEK(LogFile.LEN);
    LogFile.WRITE(FORMAT(0D)+Szoveg);
    LogFile.CLOSE;
    

    Hope it helps! :D
  • Options
    lawlaw Member Posts: 19
    Thanks for your help.
    I merged your solutions, and it works :D
Sign In or Register to comment.