Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Writing messages generated during execution to a file.

Posted on by

Hello,

Please suggest how can i write all info, warning & error messages generated while running a class to text file  like log file.

I was thinking if all the messages are written to infolog , if that be the case  then i should extract them  from infolog & write to a file using IO, is my understanding correct?

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Writing messages generated during execution to a file.

    Mav, please note that you never replied to my repeated request for you to decide where you need to run your code. You can't just randomly choose either client or server, because it has huge consequences on the actual behavior.

    Regarding the problem with running code on server, I explained it twice above. If you later find that you need code on server, you can come back and look at this thread again.

  • Verified answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Writing messages generated during execution to a file.

    Hi Mav,

    AifInfolog should be created before you run any logic. In new method it saves the current infolog line number.

    1. Instantiate AifInfolog variable

    2. Run logic

    3. Run writeMessages2File and use a variable from point 1 to call getInfoLogData

  • Mav Profile Picture
    Mav on at
    RE: Writing messages generated during execution to a file.

    HI all

    After i changed to client mode, I was able to resolve the part of generating & writing to text file after my class is executed, i tested with a error scenario & could see a text file being generated with Hello World.

    Only issue i am having now is that AifInfoLog  is blank whereas it is suppose to capture all the messages generated during my code execution.

    Does AifInfoLog   not capture all messages during code execution , if yes then please suggest a fix & if no then please suggest what should i use in place of AifInfolog so as to write all messages generated during my code execution to a text file.

    Thanks in advance.

    Shared below is my revised code.

    protected void writeMessages2File(FilePath _filepath)
    {
    //Read logs & store in txt
       AifInfoLog  lAifInfoLog = new AifInfoLog();
       #file
       TextIo txIoRead, txIoWrite;
       container conAllMessages;
       str sTempPath,sFileName = _filepath,sOneRecord;
       sFileName = "Log.txt";
       
         conAllMessages =  lAifInfoLog.getInfoLogData();
          sTempPath = _filepath ;
           if (WINAPI::fileExists(sFileName))
            {
                WINAPI::deleteFile(sTempPath   sFileName);
            }
            txIoWrite = new TextIo (@sTempPath   sFileName, 'w');
            txIoWrite.write ("HELLO WORLD");
            txIoWrite.writeExp(conAllMessages);
            txIoWrite = null;
    }

  • Mav Profile Picture
    Mav on at
    RE: Writing messages generated during execution to a file.

    @Martin Not sure what made you write if I read answers at all.

    if you go through the post I clearly explained that when running as client  It errors out at this line txIoWrite.write(conAllMessages);  Error message is TextIo object not initialized.

    It is then when all suggestions started pouring in for running at server end .

    So I switched to that and then shared errors I got after switching to server end.

    i guess the right approach would be to run it as it was running before I.e client and pls share solution for error it is getting while writing the log file In client mode  as mentioned in post of Feb27 12 05 pm.

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Writing messages generated during execution to a file.

    As I already explained above (do you read our answers at all?), code running on server requires code access permissions. If you take a piece of code written for the client tier where permissions are not set (because there aren't required there) and you want to run it on server, it's up to you to add these permissions (e.g. FileIOPermission.assert()).

    Your path looks wrong too - the AOS account would have to have access to Mav's profile folder on all AOS servers and you would end up with multiple files if you have more AOS servers (which is the typically case in production). If you wan to run code on server, you must fix your architecture first, and if you want to run your code on client, you're now wasting time with a completely wrong problem.

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Writing messages generated during execution to a file.

    And does the AOS service account have access to your user's folder?

  • Mav Profile Picture
    Mav on at
    RE: Writing messages generated during execution to a file.

    I hope we are on same page

    Issue is on 1st IO which is for reading the file , this issue was never there when running as called from or client.

    Debugger values for filename , filetype & filepath shared below.

    Filename=tsv

    FileType=.png

    FilePath=C:\Users\Mav\Desktop\Test\

    I have access right to this folder to create files.

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Writing messages generated during execution to a file.

    Hi Mav,

    Could you please share the final filename (path + filename) from the debugger? Also, try to double-check if it's not an access issue and put temp folder as _filepath for instance (c:\temp)

  • Mav Profile Picture
    Mav on at
    RE: Writing messages generated during execution to a file.

    Yes , i m running it in my devVM so that should be fine.

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Writing messages generated during execution to a file.

    Hi Mav,

    Have you checked that _filepath + _filename is the correct filename in the end and accessible on the server where AOS is installed for AOS service user?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans