Best Windows Hosting

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 11 July 2012

How to create Logfiles in Delphi?

Posted on 19:37 by Unknown
Manytimes in your delphi project, you will require to print logfiles in order to track the flow of control. Here is a simple code snippet to print logfiles in delphi.

Code snippet

procedure TMyClass.CreateLogfile;
var
  aLine : string;
  aFileName : string;
  myFile : TextFile;
  aFilePath : string;
begin
  try
    aLine := 'The line which you want to print. You can change this line dynamically by passing   aLine as  parameter to CreateLogfile function';
    aFileName := 'MyLogfile_' + FormatDateTime('dd-mm-yyyy',Now)+'.log';
    aFilePath := 'C:\ProjectFolder\'+aFileName;
    AssignFile(myFile, aFilePath);
    if FileExists(aFilePath) then
      Append(myFile)
    else
      Rewrite(myFile);
    WriteLn(myFile, FormatDateTime('dd-mmm-yyyy hh:nn:ss.zzz',Now) + ': ' + ALine);
    Flush(myFile);
  finally
    CloseFile(myFile);
  end;
end;


Explanation: CreatLogfile function creates a logfile MyLogfile_TodaysDateTime.log at C:\ProjectFolder. If the logfile exists, it will append the line at the end otherwise it will create a new logfile and then writes to it. Logfile will be created new on everyday as the DateTime will get changed everyday. Thats why, DateTime is embedded with the name of logfiles. If you don't want to create a new logfile everyday, just remove the datetime field from the name of logfile and keep it simple like MyLogfile.log.

I think, you have understood my point. For any doubts, just inform me. I will be pleased to help you.
Email ThisBlogThis!Share to XShare to Facebook
Posted in Delphi | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • 13 Things to keep in mind before using DLL in Delphi
    Keep in mind the following tips when writing your DLL: 1. Make sure you use the proper calling convention (C or stdcall). 2. Know the correc...
  • How to use TADOTable in Delphi XE2?
    Following is the code snippet which will show you how to use TADOTable in Delphi XE2? procedure TClass1.GetDataFromADOTable; begin   try    ...
  • How to use FindComponent function in Delphi XE2?
    Following is the code snippet which will show you how to use FindComponent in Delphi XE2? procedure TClass1.UseFindComponent(FieldName : str...
  • Online Finance Degrees
    There is a great demand for professionals with profound knowledge of finance and accounting in most of the reputed banks and financial insti...
  • How to grab the recruiter’s attention with your resume?
    Did you know that the average recruiter spends about 8 to 10 seconds glancing at your resume before s/he moves on to the next? So, whether y...
  • 5 ways to handle workload at your workplace
    With bigger workloads, tighter deadlines and more pressure, the temptation to pack in as many tasks as possible is hard to resist. But juggl...
  • Online Marketing Degrees
    Because global competition has become so intense, it should come as no surprise that companies invest heavily in their marketing and promoti...
  • Frameset, Frame and IFrame Elements in HTML
    Frame Element With frames, you can display more than one HTML document in the same browser window. Each HTML document is called a frame, and...
  • Oracle Streams: An Overview
    Oracle Streams enables information sharing. Each unit of shared information is called a message. The stream can propagate information within...
  • Phonegap: An amazing combination of HTML5, CSS3 and Javascript
    Phonegap (Cordova) = HTML5 + CSS3 + Javascript What a great combination!! How easy is Phonegap to learn!!! A great enhancement in mobile tec...

Categories

  • AJAX
  • C++
  • CSS
  • Delphi
  • DOTNET
  • HTML
  • Javascript
  • jQuery
  • Management
  • Online Degrees
  • Oracle
  • Others
  • Phonegap
  • PHP
  • Unix
  • XML

Blog Archive

  • ▼  2012 (155)
    • ►  September (64)
    • ►  August (11)
    • ▼  July (4)
      • How to create dynamic tables in HTML using javascr...
      • How to make most of the To-Do list?
      • VPN Technology: Virtual Private Networks
      • How to create Logfiles in Delphi?
    • ►  June (3)
    • ►  May (25)
    • ►  April (48)
Powered by Blogger.

About Me

Unknown
View my complete profile