First, VB language, development tools VB6.0
Private SubAppendtofile (ByValFn as String,ByValMsg as String) DimLfilehandle as LongLfilehandle=FreeFile on Error GoToappend_error Open fn forAppend as#lFileHandlePrint#lFileHandle, msg Close #lFileHandleExit SubAppend_error:End Sub
Log 1
Private FunctionWritelog (ByValStrlog as String) DimstrFileName as StringstrFileName="C:\Log"& VBA. Format (now,"YYYYMMDD") &". txt"Open strFileName forAppend as 1 Write#1, Now &" "&Strlog CloseEnd Function
Log 2
Public SubWritelogfile (ByValMsg as String) DimLfilehandle as Long DimSFile as String on Error Resume NextSFile= App.Path &"\xx.log"Lfilehandle=FreeFile on Error GoToappend_error Open sFile forAppend as#lFileHandlePrint#lFileHandle, now &" "&msg Close #lFileHandleExit SubAppend_error:End Sub
Log 3
--limit the size, number of days of the log Public SubWritefilelog (ByValVvalue as String) DimLhandle as Long Dimspath as String DimI as Long DimsFileName as String on Error Resume NextLhandle=FreeFile() spath= App.Path &"\log" If Dir(spath, vbdirectory) ="" Then MkDirspathEnd If 'Delete logs for more than 10 days fori =- - to-TensFileName= spath &"\xx"&Format(DATEADD("D"IDate),"YYYYMMDD") &". Log" If Dir(sFileName) <>"" Then KillsFileNameEnd If Nextspath= spath &"\xx"&Format(Date,"YYYYMMDD") &". Log" If Dir(spath) <>"" Then 'More than 500K, delete, so as not to affect performance If FileLen(spath)/1024x768> - Then KillsFileNameEnd If End IfOpen spath forAppend as#lHandlePrint#lHandle,Format(Now,"YYYY-MM-DD HH:mm:ss") & VbTab & Vvalue &vbCrLf Close #lHandleEnd SubLog 4
Second, C + + language, development tools VC6.0
voidCnotectrl::writelog (CString &csinfo) { return ; HANDLE hfile=Invalid_handle_value; CString cstmp; TCHAR*szfilename = _t ("C:\\xx.log"); hfile=CreateFile (szFileName, Generic_write, File_share_write, NULL , Open_always, File_attribute_normal, NULL); if(hfile = =Invalid_handle_value) {cstmp. Format ("Open log file err%d\n", GetLastError ()); AfxMessageBox (CSTMP); return ; } Else{DWORD Dwwritelen=0 ; SetFilePointer (hfile,0,0, File_end); if(WriteFile (Hfile,csinfo.getbuffer (Csinfo.getlength ()), Csinfo.getlength (), &dwwritelen,null) = =0) {cstmp. Format ("Write log file err%d\n", GetLastError ()); AfxMessageBox (CSTMP); CloseHandle (hfile); return ; } closehandle (hfile); }}Log 1
voidCserverthreadpool::writelog (LPCTSTR pformat, ...) {TCHAR chmsg[ +]; chmsg[0] =0; SYSTEMTIME St; Getlocaltime (&St); TCHAR szbuf[ the]; GetDateFormat (Locale_system_default, LOCALE_USE_CP_ACP,&st, _t ("YYYY-MM-DD"), Szbuf, the); Lstrcat (chmsg, szbuf); Gettimeformat (Locale_system_default, LOCALE_USE_CP_ACP,&st, _t ("HH:mm:ss"), Szbuf, the); Lstrcat (chmsg, szbuf); Va_list parg; Va_start (PARG, Pformat); _vstprintf (Szbuf, Pformat, PARG); Va_end (PARG); Lstrcat (chmsg, szbuf); Lstrcat (Chmsg, _t ("\ r \ n")); if(GetModuleFileName (NULL, Szbuf, the) {LPTSTR P= _TCSRCHR (Szbuf, _t ('\\')); p[1] =0; Lstrcat (Szbuf, _t ("XX.log")); HANDLE hfile=CreateFile (Szbuf, Generic_write, File_share_write, NULL, Open_always, File_attribute_normal, NULL); if(hfile = =Invalid_handle_value)return; SetFilePointer (hfile,0, NULL, file_end); DWORD Dwwrite; WriteFile (hfile, Chmsg, Lstrlen (chmsg)*sizeof(TCHAR), &Dwwrite, NULL); CloseHandle (hfile); } }Log 2
Third, C # language, development tools VS2010
Public voidWritelog (stringContents) { stringstrFilePath = AppDomain.CurrentDomain.BaseDirectory +"\\XX.log"; if(!file.exists (strFilePath)) {File.createtext (strFilePath). Close (); } file.appendalltext (strFilePath, DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +":"+ Contents +Environment.NewLine, System.Text.Encoding.Default); }Log 1
Public Static voidWritelog (stringmessage) { stringstrFilePath = AppDomain.CurrentDomain.BaseDirectory +"\\XX.log"; if(!file.exists (strFilePath)) {File.createtext (strFilePath). Close (); } FileStream FS=NewFileStream (strFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, fileshare.readwrite); StreamWriter W=NewStreamWriter (FS); W.basestream.seek (0, Seekorigin.end); W.writeline (DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +":"+message); W.flush (); W.close (); }Log 2
C + +, C #, VB each language log code