VB.NET Service.OnStart() 从未调用过

VB.NET Service.OnStart() Never Called

我正在使用 vb.net 来创建 windows 服务,在 OnStart() 函数中我尝试记录一个消息说它的状态,但是它接缝该函数没有被调用。 Here是完整的项目,下面是目标服务代码:

Imports System.IO

Public Class FneishSQLBackupServicev2

    Dim backupTaken As Boolean = False

    Protected Overrides Sub OnStart(ByVal args() As String)
        WriteToFile("Sql automated backup service started")
        Try
            Timer1.Start()
        Catch ex As Exception
            WriteToFile(ex.StackTrace)
        End Try
    End Sub

    Protected Overrides Sub OnStop()
        WriteToFile("Sql automated backup service stopped")
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        WriteToFile("timer entered")
        If Date.Now.Hour = My.Settings.AutoBackupTime Then
            If backupTaken = False Then
                backupTaken = True
                TakeBackup()
            End If
        Else
            backupTaken = False
        End If
    End Sub

    Public Shared Sub WriteToFile(Message As String)
        Try
            Dim path As String = System.IO.Path.GetTempPath() + "\ServiceLog"

            If Directory.Exists(path) = False Then
                Directory.CreateDirectory(path)
            End If

            Dim filepath As String = System.IO.Path.GetTempPath() & "\ServiceLog\Log_" & DateTime.Now.Date.ToShortDateString.Replace("/".ToCharArray.GetValue(0), "_".ToCharArray.GetValue(0)) & ".txt"

            Dim log As System.IO.StreamWriter

            If File.Exists(filepath) = False Then
                log = File.CreateText(filepath)
            Else
                log = My.Computer.FileSystem.OpenTextFileWriter(filepath, True)
            End If

            Message = Date.Now.ToLocalTime & vbNewLine & Message & vbNewLine
            log.WriteLine(Message)
            log.Close()
        Catch ex As Exception

        End Try
    End Sub

End Class

有什么帮助吗? 谢谢

在使用 EventLog 进行更多搜索和调试后,我发现它已成功启动,但问题出在 WriteToFile 函数中,该函数正在写入另一个文件夹,因为 System.IO.Path.GetTempPath() 正在返回 "C:\Windows\Temp" 而不是 "C:\users\myuser\appdata\local\temp" 当被服务调用时。