Can anyone please help on how to create log file in Visual C++ using MFC????

log files are just simple text files. I usually put the date/time at the beginning of the log entry. I wouldn't bother with any of the MFC file functions such as CFile and CArchive. Use normal fstream objects. The log function should open the log file, write the log entry then close it again.

Thanks for your reply..
Can you please give a simple example codes??

This has not been compiled or tested, so it might or might not contain bugs.

void log(const char*msg)
    time_t now = time(0);
    struct tm* tm = localtime(&now);    
    ofstream out( "logfile.txt",ios::ate);
    out << tm->tm_year << '/' << tm->tm_mon << '/' << tm->tm_mday
         << ' ' << tm->tm_hour << ':' << tm->tm_min << ':' << tm->tm_sec << ": ";
    out << msg << "\n";

but this code never work out under MFC.... i had include fstrem.h and iostream.h???
any other suggestions???

you just need to include

#include <fstream>
#include <iostream>

don't use .h suffix.
also encapsulate the Dragon's function in some class and make that a static member.

and your simplest logger is ready :).

you can also make it into a class like so

class DSErrorLog
    FILE* LogHandle;
	char szLogName[32];
	char szTimeStamp[64];
    char szLastDeleted[32];  

    void WriteToLog(char* szText);
    void ErrorLogPurge(int DaysOld);
    void WriteError(char* szText, char* File, int Line);
	void CreateTimeStamp(char* Format, bool Millisecs);
	bool __cdecl WriteToLogFormatted(const char* format, ...);

and just use the file io funcs from cstdio(i prefer them over fstream, especially as you can use the sprintf formatting, much faster)

This article has been dead for over six months. Start a new discussion instead.