Logging to textfile

law
Member Posts: 19
Hi
I have to log some events to a text file with C/AL.
I write this proc:
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?)
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?)
0
Comments
-
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;
0 -
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 !0 -
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 setLogFile.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!0 -
Thanks for your help.
I merged your solutions, and it works0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions