[英]VB.NET Service.OnStart() Never Called
我正在使用vb.net來創建Windows服務,在該服務上,我試圖在OnStart()函數上記錄一條消息,說明其狀態,但是它表明該函數未被調用。 這是完整的項目,下面是目標服務代碼:
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"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.