【问题标题】:VB6-how to create log file in VB6 while launching applicationVB6-如何在启动应用程序时在 VB6 中创建日志文件
【发布时间】:2012-10-19 00:06:07
【问题描述】:

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

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

谢谢..

【问题讨论】:

标签: logging vb6


【解决方案1】:

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

您还需要一个将错误详细信息记录到文件中的通用例程,如下所示。健康警告 - 我只是直接输入了这个没有测试它(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

奖励建议:roll your own stack trace.
额外建议2:使用here 中的IsInIDE 函数,使用If Not IsInIDE() Then On Error Goto Handler 之类的东西关闭IDE 中的错误处理程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多