[英]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.