I'm using vb.net in order to create a windows service, where on the OnStart() function I'm trying to log a message saying its status, however it seams that the function is not being called. Here is the complete project and Below is the target service code:
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
Any help please? Thanks
在使用EventLog
进行更多搜索和调试之后,我发现它已成功启动,但是问题出在写入另一个文件夹的WriteToFile
函数中,因为System.IO.Path.GetTempPath()
返回的是"C:\\Windows\\Temp"
而不是服务调用时为"C:\\users\\myuser\\appdata\\local\\temp"
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.