VB6-如何在启动应用程序时在VB6中创建日志文件 [英] VB6-how to create log file in VB6 while launching application

查看:44
本文介绍了VB6-如何在启动应用程序时在VB6中创建日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录在我的应用程序执行期间发生的异常.在此之前,我使用消息框对其进行了处理.我是VB 6的新手.

I want to log the exceptions occurred during execution of my app. Before this I handled it with message box. I am new to VB 6.

请提供一些示例代码来创建日志文件并保存异常消息.

Please provide some sample code to create log file and to save exception messages.

谢谢..

推荐答案

您需要使用 On Error Goto 的错误处理程序,以便在发生错误时可以执行自己的代码.(在VB6中,顺便说一句,它们被称为错误而不是例外.)免费工具 MZTools 非常好-它可以自动插入 On Error Goto 和错误处理程序,其中包括当前例程的名称.

You need error handlers, using On Error Goto, so that you can execute your own code when an error occurs. (BTW in VB6 they are called errors not exceptions.) The free tool MZTools is excellent - it can automatically insert the On Error Goto and an error handler which includes the name of the current routine.

您还需要一个将错误详细信息记录到文件中的常规例程,如下所示.健康警告-我只是直接输入而不进行测试(

You also need a general routine that logs error details to a file, a little bit like this below. Health warning - I've just typed this straight in without testing it (air code).

Sub MySub()
  On Error Goto ErrHandler
  '... Do something ...'
  On Error Goto 0
  Exit Sub

ErrHandler:
  Call LogError("MySub", Err, Error$) ' passes name of current routine '
End Sub 

' General routine for logging errors '
Sub LogError(ProcName$, ErrNum&, ErrorMsg$)
  On Error Goto ErrHandler
  Dim nUnit As Integer
  nUnit = FreeFile
  ' This assumes write access to the directory containing the program '
  ' You will need to choose another directory if this is not possible '
  Open App.Path & App.ExeName & ".log" For Append As nUnit
  Print #nUnit, "Error in " & ProcName
  Print #nUnit, "  " & ErrNum & ", " & ErrorMsg
  Print #nUnit, "  " & Format$(Now)
  Print #nUnit
  Close nUnit
  Exit Sub

ErrHandler:
  'Failed to write log for some reason.'
  'Show MsgBox so error does not go unreported '
  MsgBox "Error in " & ProcName & vbNewLine & _
    ErrNum & ", " & ErrorMsg
End Sub

奖励建议:滚动自己的堆栈跟踪记录.
奖励建议2:使用类似 If Not IsInIDE()然后关闭错误的To Go处理程序,使用此处

这篇关于VB6-如何在启动应用程序时在VB6中创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆