在VB中查找函数中的错误

时间:2014-03-18 11:45:34

标签: vb.net error-handling vb.net-2010

我试图在Timer.Elapsed上调用另一个类中的函数。我可以很好地调用测试函数,但是当我调用我想要的实际函数时,我没有例外,但函数不会运行。我尝试添加一些错误处理(Catch ex)并将频繁的消息输出到.txt文件以查看它失败的地方,但是当我知道我用来写的函数时,我没有在我的日志中收到任何这些消息这些消息到日志正在运行。

如果我无权访问错误消息,如何找到我的函数包含错误的位置?

在下面添加我的代码 - 这是我写入日志功能。

Public Shared Function Output_To_Log(ByVal message As String) As String

    Dim strDate As String = Now.ToString("dd MMM yy HH:mm:ss ")
    Dim strTodayDate As String = Now.ToString("yyyyMMMdd")

    Dim file As New FileStream("C:\PHJones_Windows_Service\logs\Log" & strTodayDate & ".txt", FileMode.Append, FileAccess.Write)
    Dim stream As New StreamWriter(file)
    stream.WriteLine(message & " : " & strDate)
    stream.Close()

    Return ""

End Function

这是我的计时器已用完的功能。

Private Shared Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Output_To_Log("Working")

    PHJones.Start_Batch()

End Sub

这是我的Start_Batch函数,对我的服务器的引用被****

消隐了
Public Shared Function Start_Batch() As Integer
    Try

        Dim a As New running_check
        a.check = 1

        Dim files As String()
        Dim File As String
        Dim myProcess As New Diagnostics.Process()
        Dim File_Name As String
        Dim Running_FileName As String

        RunTimer.Output_To_Log("Start_Batch starting")

Start:
        RunTimer.Output_To_Log("Checking logs")
        Dim log_check As Integer = check_logs()

        RunTimer.Output_To_Log("Getting .DAT files.")
        files = IO.Directory.GetFiles("****\phjones\to_phjones\", "*.DAT")

        If files.Count > 0 Then
            RunTimer.Output_To_Log("Counted " & files.Count & " files.")
        Else
            RunTimer.Output_To_Log("No files found.")
        End If

        For Each File In files
            Try
                RunTimer.Output_To_Log("Starting process for " & File)
                Running_FileName = File & ".BAT"
                RunTimer.Output_To_Log("Processing " & Running_FileName)

                File_Name = File.Substring(26)

                If System.IO.File.Exists("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT") Then
                    RunTimer.Output_To_Log("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT already exists")
                Else
                    RunTimer.Output_To_Log("Copying " & Running_FileName & " to batch_files folder")
                    System.IO.File.Copy(Running_FileName, "C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT", True)
                End If

                If (System.IO.File.Exists("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT")) Then
                    If (System.IO.File.Exists(Running_FileName)) Then
                        RunTimer.Output_To_Log("Deleting file " & Running_FileName)
                        System.IO.File.Delete(Running_FileName)
                    Else
                        RunTimer.Output_To_Log(File_Name & ".BAT does not exist in ****\phjones\to_phjones\processed")
                    End If
                Else
                    RunTimer.Output_To_Log(File_Name & ".BAT failed to copy")
                    Throw New Exception(File_Name & ".BAT failed to copy to C:\PHJones_Windows_Service\Batch_Files")
                End If

                RunTimer.Output_To_Log("Executing batch file " & Running_FileName)
                myProcess.StartInfo.UseShellExecute = True
                myProcess.StartInfo.FileName = "C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT"
                myProcess.StartInfo.CreateNoWindow = False
                myProcess.Start()
                myProcess.WaitForExit()

                If System.IO.File.Exists("****\phjones\to_phjones\" & File_Name) Then
                    RunTimer.Output_To_Log("****\phjones\to_phjones\" & File_Name & " already exists")
                    System.IO.File.Delete(File)
                    RunTimer.Output_To_Log(File & " has been deleted")
                Else
                    RunTimer.Output_To_Log("Moving " & File)
                    System.IO.File.Move(File, "****\phjones\to_phjones\" & File_Name)
                End If

                Dim IWCnn = New OracleConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)

                Dim intRepair_Id As Integer = Mid(File_Name, 1, 7)
                Dim intRepair_seq As Integer = Mid(File_Name, 8, 1)

                RunTimer.Output_To_Log("Updating database for file " & File)
                IWCnn.Open()
                Dim StatusCmd As New OracleCommand("update works_orders " & _
                                    "set wor_sco_code = 'ISS', wor_issued_datetime = sysdate" & _
                                    " where wor_srq_no = " & intRepair_Id & _
                                    " and wor_seqno  = " & intRepair_seq, IWCnn)
                StatusCmd.ExecuteNonQuery()
                IWCnn.Close()

            Catch ex As Exception

                RunTimer.Timer1.Enabled = False

                RunTimer.Output_To_Log("Exception thrown in PHJones 2010 - " & ex.Message)



                Thread.Sleep(900000)
                RunTimer.Timer1.Enabled = True

                a.check = 0

                Return 0

            End Try
        Next

        a.check = 0

    Catch ex As Exception
        RunTimer.Output_To_Log("Exception thrown in PHJones 2010 - " & ex.Message)
    End Try


    Return 0


End Function

整个RunTimer类。

Imports System.Configuration.ConfigurationSettings
Imports System.Data
Imports System.IO
Imports System.Diagnostics
Imports System
Imports System.Timers
Imports System.Threading
Imports System.ServiceProcess
Imports System.Configuration.Install


Public Class RunTimer

Inherits System.ServiceProcess.ServiceBase

Friend Shared WithEvents Timer1 As System.Timers.Timer

Public Counter As Integer = 0

Public Sub New()

    MyBase.New()
    InitializeComponents()

End Sub



Private Sub InitializeComponents()

    Me.ServiceName = "RunTimer"
    Me.AutoLog = True
    Me.CanStop = True
    Timer1 = New System.Timers.Timer()
    Timer1.Interval = 15000
    Timer1.Enabled = True

End Sub



' This method starts the service.

<MTAThread()> Shared Sub Main()

    ' To run more than one service you have to add them to the array

    System.ServiceProcess.ServiceBase.Run(New System.ServiceProcess.ServiceBase() {New RunTimer})

End Sub



' Clean up any resources being used.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

    MyBase.Dispose(disposing)

    ' TODO: Add cleanup code here (if required)

End Sub



Protected Overrides Sub OnStart(ByVal args() As String)

    ' TODO: Add start code here (if required) to start your service.

    Timer1.Enabled = True

End Sub



Protected Overrides Sub OnStop()

    ' TODO: Add tear-down code here (if required) to stop your service.
    Timer1.Enabled = False
    Output_To_Log("Ended")

End Sub


Private Sub InitializeComponent()


    Timer1 = New System.Timers.Timer

    CType(Timer1, System.ComponentModel.ISupportInitialize).BeginInit()

    Timer1.Enabled = True

    CType(Timer1, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

Private Shared Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Output_To_Log("Working")

    PHJones.Start_Batch()

End Sub

1 个答案:

答案 0 :(得分:1)

由于您作为服务运行,因此您不会看到普通的错误消息。 Timer事件中的Output_To_Log("Working")RunTimer.Output_To_Log("Start_Batch starting")的{​​{1}}之间可能存在错误。例如,如果Start_Batch()的初始化或呼叫本身Dim a As New running_check中可能发生错误。这些都会导致你所看到的。