C++、C#、VB各语言日志代码
一、VB语言,开发工具VB6.0
Private Sub AppendToFile(ByVal fn As String, ByVal msg As String) Dim lFileHandle As Long lFileHandle = FreeFile On Error GoTo Append_Error Open fn For Append As #lFileHandle Print #lFileHandle, msg Close #lFileHandle Exit Sub Append_Error: End Sub
Private Function WriteLog(ByVal strLog As String) Dim strFileName As String strFileName = "C:\Log" & VBA.Format(Now, "yyyymmdd") & ".txt" Open strFileName For Append As 1 Write #1, Now & " " & strLog Close End Function
Public Sub WriteLogFile(ByVal msg As String) Dim lFileHandle As Long Dim sFile As String On Error Resume Next sFile = App.Path & "\XX.log" lFileHandle = FreeFile On Error GoTo Append_Error Open sFile For Append As #lFileHandle Print #lFileHandle, Now & " " & msg Close #lFileHandle Exit Sub Append_Error: End Sub
--限制大小、天数的日志 Public Sub WriteFileLog(ByVal vValue As String) Dim lHandle As Long Dim sPath As String Dim i As Long Dim sFilename As String On Error Resume Next lHandle = FreeFile() sPath = App.Path & "\Log" If Dir(sPath, vbDirectory) = "" Then MkDir sPath End If '删除超过10天的日志 For i = -20 To -10 sFilename = sPath & "\XX" & Format(DateAdd("d", i, Date), "YYYYMMDD") & ".log" If Dir(sFilename) <> "" Then Kill sFilename End If Next sPath = sPath & "\XX" & Format(Date, "YYYYMMDD") & ".log" If Dir(sPath) <> "" Then '大于500K,就删除,以免影响性能 If FileLen(sPath) / 1024 > 500 Then Kill sFilename End If End If Open sPath For Append As #lHandle Print #lHandle, Format(Now, "YYYY-MM-DD HH:mm:ss") & vbTab & vValue & vbCrLf Close #lHandle End Sub
二、C++语言,开发工具VC6.0
void CNoteCtrl::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); } }
void CServerThreadPool::WriteLog(LPCTSTR pFormat, ...) { TCHAR chMsg[512]; chMsg[0] = 0; SYSTEMTIME st; GetLocalTime(&st); TCHAR szBuf[256]; GetDateFormat(LOCALE_SYSTEM_DEFAULT, LOCALE_USE_CP_ACP, &st, _T("yyyy-MM-dd "), szBuf, 256); lstrcat(chMsg, szBuf); GetTimeFormat(LOCALE_SYSTEM_DEFAULT, LOCALE_USE_CP_ACP, &st, _T("HH:mm:ss "), szBuf, 256); 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, 256)) { 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); } }
三、C#语言,开发工具VS2010
public void WriteLog(string Contents) { string strFilePath = 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); }
public static void WriteLog(string message) { string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\XX.log"; if (!File.Exists(strFilePath)) { File.CreateText(strFilePath).Close(); } FileStream fs = new FileStream(strFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter w = new StreamWriter(fs); w.BaseStream.Seek(0, SeekOrigin.End); w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + ":" + message); w.Flush(); w.Close(); }