I have developed a service with vb.net.
This service is been installed from a windows application which close immediately after the Service starts.
Inside the service I have a procedure “Protected Overrides Sub OnCustomCommand
“, this procedure works fine until the pointer comes to instruction which I give you below:
Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
StartLogFile("Custom Command {" & command & "} invoked", EventLogEntryType.Information)
Dim Position As String
Dim myFile As String = Nothing
Dim myTime As String = Nothing
Try
If command = SimpleServiceCustomCommands.StartWorker Then
Position = "GetKeyValue Time"
myTime = GetKeyValue("Time", "", RegPath)
Position = "GetKeyValue Name"
myFile = GetKeyValue("Name", "", RegPath)
StartLogFile("Notice OnCustomCommand" & vbNewLine & "MyTime= { " & myTime & "}, {MyFile= { " & myFile & "} ", EventLogEntryType.Error)
.
.
.
Catch ex As Exception
StartLogFile("Error OnCustomCommand" & vbNewLine & Position & vbNewLine & ex.StackTrace, EventLogEntryType.Error)
Finally
End Try
I made some changes according to notices i read and now the GetKeyValue did not returns me any error. But either did not returns me any value.
The function Get is as follows:
Public Function GetKeyValue(ByVal nKey As String, ByVal vKey As String, ByVal sPath As String) As String
Dim RegKey As RegistryKey
Dim kValue As String = Nothing
Dim Pos As String
If RegKeyExists(sPath) Then
Try
RegKey = Registry.CurrentUser.OpenSubKey(sPath, True)
kValue = CStr(RegKey.GetValue(nKey))
Catch ex As Exception
StartLogFile(" GetKeyValue, RegKeyExists(sPath) " & vbNewLine & "{ RegKey= " & RegKey.Name & "}, { kValue= " & kValue & "}, { nKey= " & nKey & "}" & vbNewLine & "Stack Trace= " & ex.StackTrace, EventLogEntryType.Warning)
End Try
End If
Return kValue
End Function
Now what I have is:
No Error, No Values
!!!!
Your code obviously didn’t fail inside the try-catch statement inside GetKeyValue() because then you’ll see from the log that the error came from inside that function itself.
So your code must be either be failing on the “False” case, or form the StartLogFile() call in the “True” case’s catch block.
False Case
Notice that you have
RegKey.Namein your log statement, andRegKeyis very obviouslynullbecause you never set it (it is only set in the True case). I think this is where your error is.True Case Catch Block
The catch block in your “True” case does not access any property of any variable that may be null. However, StartLogFile may itself be failing with such an execption inside.
Swapped statements
I have a feeling that you have swapped the two StartLogFile() calls (the False case one should be in the catch block of the True case, and the one in the catch block of the True case should be in the False case).
Avoid error logging that may itself fail
Also, even in the True case block, you cannot assume
RegKeyis non-null becauseRegistry.CurrentUser.OpenSubKeymay fail and you’ll not understand where the error is because your try-catch block catches the fault and the it fails on your error logging statement instead! This type of bug can be very frustrating to find, especially in large systems. The morale: never make your error logging code too complex — if they fail, they make it difficult to find out what happened.Always include stack trace in error log
Also, is there any reason why you don’t include a stack trace in your error logging? Having a stack trace will immediately pin-point you to the problem code line.