Вопрос

I'm writing an error logging routine that will be used by multiple WCF Service Providers (hosted in a background Windows Service), so that they can log whatever exception they catch. Thing is, I don't know what to do if I am the one catching an exception within my routine.

To be more specific, here is my current routine (in VB.Net):

Public Shared Sub LogError(Text As String, Filename As String, PathLog As String)
    Try
        PathLog = Path.GetFullPath(PathLog)

        If Not Directory.Exists(PathLog) Then
            Directory.CreateDirectory(PathLog)
        End If

        Dim PathFile As String = Path.GetFullPath(PathLog & "\" & Filename)
        File.AppendAllText(PathFile, "[" & GetDateTimeWithMiliseconds() & "] " & vbNewLine & Text & vbNewLine)
    Catch ex As Exception
        'ToDo: WHAT TO DO?!
    End Try
End Sub

I realize I should probably catch the likely exceptions individually, instead of doing a general Try...Catch. But my point is, what do I do once I catch them?

  1. I cannot throw a MessageBox, because it's a Windows Service hosted WCF Server (and even if I could, I shouldn't)
  2. I will most likely not be able to log the error, because the error logging routine is already failing

I'm in a jam

P.S: External logging solutions are out of the question

Это было полезно?

Решение

Every program in this world has a point at which you have to say, "I can't do anything about it". And this is how you say this:

 Try
 .....
 Catch
    ''Do Nothing
 End Try

So, you may want to write to event log about your special logging failure. But if event log fails, you can't do nothing about, and the code above will take care of that.

In your concrete example, you should use a lock in your method because multiple threads may access same code and the file may be locked, which will cause the error.

SyncLock lockObject
       '' your code
End SyncLock 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top