工作中免不了需要为自己的程序添加日志,我也从网上扒拉了一个老外写的模块,修改修改了下,凑合用吧。

 Option Explicit
'**************************************
' 模块名称: AppendToLog 通过API写入日志
'**************************************
'API 声明
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const Create_NEW =
Private Const OPEN_EXISTING =
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_BEGIN =
Private Const INVALID_HANDLE_VALUE = -
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '调用:Call AppendToLog("测试模块名","测试日志内容")
'**************************************
' 方法名称: AppendToLog
' 输入参数:sMdl 模块名称 sMessage 日志内容
'**************************************
Public Sub AppendToLog(sMdl As String, sMessage As String) On Error GoTo Err: '获取计算机名、用户名、本机ip
Dim LocalInfo As String
Dim strLocalIP As String
Dim winIP As Object
LocalInfo = LocalInfo & " Computer:" & Environ("computername")
LocalInfo = LocalInfo & " User:" & Environ("username")
Set winIP = CreateObject("MSWinsock.Winsock")
strLocalIP = winIP.LocalIP
LocalInfo = LocalInfo & " IP:" & strLocalIP Dim lpFileName As String
lpFileName = App.Path + "\Log"
If Dir(lpFileName, vbDirectory) = "" Then
MkDir (lpFileName)
End If lpFileName = lpFileName + "\" + Format(Now, "yyyymmdd") + ".log" sMessage = "--" + Format(Now, "yyyy-mm-dd hh:mm:ss") + " 模块:" + sMdl + LocalInfo + vbNewLine + sMessage + vbNewLine
'appends a string to a text file.
'it's up to the coder to add a CR/LF at the end
'of the string if (s)he so desires.
'assume failure
'AppendToLog = False
'exit if the string cannot be written to disk
If Len(sMessage) < Then Exit Sub
'get the size of the file (if it exists)
Dim fLen As Long: fLen =
If (Len(Dir(lpFileName))) Then: fLen = FileLen(lpFileName)
'open the log file, create as necessary
Dim hLogFile As Long
hLogFile = CreateFile(lpFileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal &, _
IIf(Len(Dir(lpFileName)), OPEN_EXISTING, Create_NEW), _
FILE_ATTRIBUTE_NORMAL, &)
'ensure the log file was opened properly
If (hLogFile = INVALID_HANDLE_VALUE) Then Exit Sub
'move file pointer to end of file if file was not created
If (fLen <> ) Then
If (SetFilePointer(hLogFile, fLen, ByVal &, FILE_BEGIN) = &HFFFFFFFF) Then
'exit sub if the pointer did not set correctly
CloseHandle (hLogFile)
Exit Sub
End If
End If
'convert the source string to a byte array for use with WriteFile
Dim lTemp As Long
ReDim TempArray( To Len(sMessage) - ) As Byte
TempArray = StrConv(sMessage, vbFromUnicode)
lTemp = UBound(TempArray) +
'write the string to the log file
If (WriteFile(hLogFile, TempArray(), lTemp, lTemp, ByVal &) <> ) Then
'the data was written correctly
'AppendToLog = True
End If
'flush buffers and close the file
FlushFileBuffers (hLogFile)
CloseHandle (hLogFile)
Exit Sub
Err:
MsgBox "日志写入出错,原因是" + Err.Description, vbExclamation, "提示信息" End Sub

最新文章

  1. Emberjs之ComputedProperty
  2. Mysql相关集锦
  3. 名词解释——Ext JS4
  4. [ubuntu]给ubuntu server安装xubuntu(xfce)窗口管理器
  5. [读书笔记]java中的类加载器
  6. Django中创建自己的Context_Processors
  7. vs 2015密钥
  8. Spark小课堂Week4 从控制台看Spark逻辑结构
  9. 转-----EasyCHM制作教程
  10. 使用react-native做一个简单的应用-02项目搭建与运行
  11. IIS7部署MVC站点后,打开无法正常跳转到首页
  12. ubuntu14.04 + OpenCV2.4.9 配置方法
  13. python实现常见排序算法
  14. dijkstra算法解决单源最短路问题
  15. iOS 钥匙串存储用户数据
  16. python识别图片
  17. NFS常见问题
  18. Linux 内核开发 - 内核定时器
  19. CentOS 6.4 命令行 安装 VMware Tools
  20. OpenVirteX 创建简易虚拟网络

热门文章

  1. Object-c中的属性和成员变量的关系详解
  2. Divisibility
  3. Selector中的各种状态详解
  4. [转]W3C 验证 there is no attribute target for this element
  5. 实现TableLayout布局下循环取出TableRow控件中的文字内容到list集合
  6. 【已解决】BeautifulSoup已经获得了Unicode的Soup但是print出来却是乱码
  7. 移动端开发的meta标签作用
  8. Redis - 发布/订阅模式
  9. WCF开发教程资源收集
  10. contentMode各种样式展示