VB6-AppendToLog 通过API写入日志

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

 1 Option Explicit
 2 ‘**************************************
 3 ‘ 模块名称: AppendToLog 通过API写入日志
 4 ‘**************************************
 5 ‘API 声明
 6 Private Const GENERIC_WRITE = &H40000000
 7 Private Const FILE_SHARE_READ = &H1
 8 Private Const Create_NEW = 1
 9 Private Const OPEN_EXISTING = 3
10 Private Const FILE_ATTRIBUTE_NORMAL = &H80
11 Private Const FILE_BEGIN = 0
12 Private Const INVALID_HANDLE_VALUE = -1
13 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
14 Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
15 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
16 Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
17 Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
18
19 ‘调用:Call AppendToLog("测试模块名","测试日志内容")
20 ‘**************************************
21 ‘ 方法名称: AppendToLog
22 ‘ 输入参数:sMdl 模块名称 sMessage 日志内容
23 ‘**************************************
24 Public Sub AppendToLog(sMdl As String, sMessage As String)
25
26 On Error GoTo Err:
27
28     ‘获取计算机名、用户名、本机ip
29     Dim LocalInfo As String
30     Dim strLocalIP As String
31     Dim winIP As Object
32     LocalInfo = LocalInfo & "  Computer:" & Environ("computername")
33     LocalInfo = LocalInfo & "  User:" & Environ("username")
34     Set winIP = CreateObject("MSWinsock.Winsock")
35     strLocalIP = winIP.LocalIP
36     LocalInfo = LocalInfo & "  IP:" & strLocalIP
37
38     Dim lpFileName As String
39     lpFileName = App.Path + "\Log"
40     If Dir(lpFileName, vbDirectory) = "" Then
41         MkDir (lpFileName)
42     End If
43
44     lpFileName = lpFileName + "\" + Format(Now, "yyyymmdd") + ".log"
45
46     sMessage = "--" + Format(Now, "yyyy-mm-dd hh:mm:ss") + "  模块:" + sMdl + LocalInfo + vbNewLine + sMessage + vbNewLine
47     ‘appends a string to a text file.
48     ‘it‘s up to the coder to add a CR/LF at the end
49     ‘of the string if (s)he so desires.
50     ‘assume failure
51     ‘AppendToLog = False
52     ‘exit if the string cannot be written to disk
53     If Len(sMessage) < 1 Then Exit Sub
54     ‘get the size of the file (if it exists)
55     Dim fLen As Long: fLen = 0
56     If (Len(Dir(lpFileName))) Then: fLen = FileLen(lpFileName)
57     ‘open the log file, create as necessary
58     Dim hLogFile As Long
59     hLogFile = CreateFile(lpFileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, _
60         IIf(Len(Dir(lpFileName)), OPEN_EXISTING, Create_NEW), _
61         FILE_ATTRIBUTE_NORMAL, 0&)
62     ‘ensure the log file was opened properly
63     If (hLogFile = INVALID_HANDLE_VALUE) Then Exit Sub
64     ‘move file pointer to end of file if file was not created
65     If (fLen <> 0) Then
66         If (SetFilePointer(hLogFile, fLen, ByVal 0&, FILE_BEGIN) = &HFFFFFFFF) Then
67             ‘exit sub if the pointer did not set correctly
68             CloseHandle (hLogFile)
69             Exit Sub
70         End If
71     End If
72     ‘convert the source string to a byte array for use with WriteFile
73     Dim lTemp As Long
74     ReDim TempArray(0 To Len(sMessage) - 1) As Byte
75     TempArray = StrConv(sMessage, vbFromUnicode)
76     lTemp = UBound(TempArray) + 1
77     ‘write the string to the log file
78     If (WriteFile(hLogFile, TempArray(0), lTemp, lTemp, ByVal 0&) <> 0) Then
79         ‘the data was written correctly
80         ‘AppendToLog = True
81     End If
82     ‘flush buffers and close the file
83     FlushFileBuffers (hLogFile)
84     CloseHandle (hLogFile)
85     Exit Sub
86 Err:
87     MsgBox "日志写入出错,原因是" + Err.Description, vbExclamation, "提示信息"
88
89 End Sub

VB6-AppendToLog 通过API写入日志

时间: 2024-10-29 19:11:28

VB6-AppendToLog 通过API写入日志的相关文章

如何给框架添加API接口日志

前言 用的公司的框架,是MVC框架,看了下里面的日志基类,是操作日志,对增删改进行记录, 夸张的是一张业务的数据表 需要一张专门的日志表进行记录, 就是说你写个更新,添加的方法都必须写一遍操作日志,代码看了,不多,6行. 不懂为什么要这样做,API 简洁万岁!~  于是果断在生命周期中插入接口日志. 思路1   一次插入 1.生命周期在未进入接口前拿到相关信息(包括:接口名称,url,参数,方法,客户端IP,访问时间,甚至类和方法等信息), 2.然后正常往下走调用接口的方法, 3.等待接口中的方

获取Android崩溃crash信息并写入日志发送邮件

一.实现Thread.UncaughtExceptionHandlerUnChecked异常发生时,由于没有相应的try…catch处理该异常对象,所以Java运行环境将会终止,程序将退出,也就是我们所说的Crash.Java API提供了一个全局异常捕获处理器,Android应用在Java层捕获Crash依赖的就是Thread.UncaughtExceptionHandler处理器接口,通常我们只需实现这个接口,并重写其中的uncaughtException方法,在该方法中可以读取Crash的

expdp导出遇到ORA-39064: 无法写入日志文件

. . 导出了 "USER_XXXX"."TAB_YYYY" 0 KB 0 行 ORA-39064: 无法写入日志文件 ORA-29285: 文件写入错误 经过搜索mos,有如下的文章给出了解决方案: Error ORA-39064 Might Be Encountered By DataPump Export (EXPDP) When NLS_LANG Is Different From Database Character Set (Doc ID 1321034

PHP error_log()将错误信息写入日志文件

error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段. bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) 把错误信息发送到 web 服务器的错误日志,或者到一个文件里. message 应该被记录的错误信息.信息长度限制:The default seem to be 1024

C# winform程序将异常写入日志的方法

转载地址:夏日里的春天的博客 http://hi.baidu.com/honfei/item/3a6f212998910099b73263b5 //出错之后计入日志文件        private void SqlConnError(SqlException e2)        {            //如果是同一天的话,则打开文件在末尾写入.如果不是同一天,则创建文件写入文件 //判断是否存在文件            if (File.Exists(DateTime.Today.To

log4net在release模式下无法生成文件或不写入日志

在Debug模式一切正常,但是在release模式下log4net不工作,查了很多资料,终于解决.具体做如下检查修改. 1.检查log4net写入日志文件路径是否正确: 2.检查对应日志文件路径是否有权限: 3.检查程序log4net配置获取路径: 最常见的问题是第三步,一般都是在AssemblyInfo.cs文件中写入如下代码 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Config

【翻译自mos文章】禁止写入日志到Sqlnet.log和Listener.log

禁止写入日志到Sqlnet.log和Listener.log 参考原文: How to Disable Logging to the Sqlnet.log and the Listener.log (Doc ID 162675.1) 适用于: Oracle Net Services Information in this document applies to any platform. Checked for relevance on 15-Jan-2012 解决方案: Disable Cli

【.net 深呼吸】写入日志文件

记录日志,一方面可以把日志写入系统的日志存储中,可在“事件查看器”窗口中查看:如果不喜欢写到系统的日志文件中,也可以写到自己定义的文件中. 其实,日志文件就是文本文件,可能有朋友会想到用写入文本文件的方式来写日志.当然,这样做也是可以的,不过,.NET 类型库提供了两个专用记录信息的类. Debug 类大家都很熟悉,就是可以在VS的“输出”窗口写入调试信息,另外,有一个跟Debug很像的类——Trace.Debug通常只会在调试版本中使用,Trace既可以在调试版本中用,也可以用于发布(Rele

python 通过logging写入日志到文件和控制台

#!/usr/bin/python #-*- coding:utf-8 -*- import logging # 创建一个logger  logger = logging.getLogger('mytest')   logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件  fh = logging.FileHandler('test.log') fh.setLevel(logging.DEBUG) # 再创建一个handler,用于输出到控制台