zoukankan      html  css  js  c++  java
  • 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
    日志1
    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
    日志2
    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
    日志3
    --限制大小、天数的日志
    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
    日志4

    二、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
    ",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
    ",GetLastError());
                AfxMessageBox(cstmp);
                CloseHandle(hFile);
                return ;
            }
            CloseHandle(hFile);
        }
    }
    日志1
    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("
    "));
        
            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);
            }    
    }
    日志2

    三、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);
            }
    日志1
    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();
            }
    日志2
  • 相关阅读:
    开课吧-孤尽公开课视频内容整理笔记
    业务系统数据库设计经验总结(七)-强制使用选定索引
    业务系统数据库设计经验总结(六)-MySQL中ORDER BY LIMIT分页数据性能问题
    业务系统数据库设计经验总结(五)-MySQL中ORDER BY LIMIT分页数据重复问题
    mongoTemplate聚合统计字段类型为字符串的数据,根据时间日期月份进行统计,日期也为字符串
    预览在线的pdf
    react页面缓存 使用本地存储
    react页面缓存插件 react-router-cache-router
    README.md的基本语法使用
    eclipse tomcat的一些错误
  • 原文地址:https://www.cnblogs.com/xbzsz/p/7532073.html
Copyright © 2011-2022 走看看