Is there anyway to simplify this code so there aren't so many redundant w = File.AppendText(filePath)
and w.flush()
and w.close()
in each if statement?
Anything else I have tried ends up locking the file, and crashing the site because to text file is in use by another process.
Public filePath As String = "c:\inetpub\logs\login.txt"
Public w As StreamWriter
Protected Sub LoginUser_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoginError
Dim UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox)
Dim CurrentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)
Dim logLoginError As New Thread(
Sub()
If (CurrentUser IsNot Nothing) Then
If (CurrentUser.IsLockedOut = True) Then
w = File.AppendText(filePath)
w.WriteLine(CurrentUser.ToString & "'s (" & IPAddress & ") account is locked out - " & DateAndTime.Now)
w.Flush()
w.Close()
ElseIf (CurrentUser.IsApproved = False) Then
w = File.AppendText(filePath)
w.WriteLine(CurrentUser.ToString & " (" & IPAddress & ") is attempting to login, but the account is disabled - " & DateAndTime.Now)
w.Flush()
w.Close()
Else
w = File.AppendText(filePath)
w.WriteLine(CurrentUser.ToString & "'s (" & IPAddress & ") login credentials failed - " & DateAndTime.Now)
w.Flush()
w.Close()
End If
Else
w = File.AppendText(filePath)
w.WriteLine("Unknown user " & UserName.Text & " (" & IPAddress & ") attempting to login - " & DateAndTime.Now)
w.Flush()
w.Close()
End If
End Sub
)
logLoginError.Start()
End Sub