Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create My Own Text Log File, Multi-User Concern

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
VB.Net Windows Forms application. I'm making my own log text file (AppName.log) to log the application errors and maybe some other stuff just to note the date/time when the event happened. Do I need to have a separate log file for each user to avoid any writing conflicts if two users need to write to the same log file at the same time? I plan on having the log file open for a short a period as possible. Maybe put the open and write in a try catch block and retry after a second or two.

Auguy
Sylvania/Toledo Ohio
 
In my app I have something like this, one file for all users:

Code:
    Private Sub SomeSubHere()

        Try
            ...
        Catch ex As Exception
            Call ErrorHandler(ex)
        End Try
    End Sub

and I have this in a standard Module:

Code:
    Public Sub ErrorHandler(ByRef ex As Exception)

        MessageBox.Show(ex.Message, "Error")

        Dim strHCErrorPath As String = "SomeErrorServerFilePath/FileError.txt"
        Dim sw As StreamWriter

        sw = File.AppendText(strHCErrorPath)
        sw.WriteLine(DateTime.Now & ", User: " & strUSERNAME & vbNewLine)
        sw.WriteLine(ex.ToString)
        sw.WriteLine(strSQL)
        sw.WriteLine("*********************************" & vbNewLine)
        sw.Flush()
        sw.Close()

    End Sub

I have the strUSERNAME declared as Public and it keeps my User's login name.
I never had a problem of 2 or more users writing to this file at once.

Have fun.

---- Andy
 
Thanks Any, that's basically what I have come up with too. I still might put the text file update in a loop with try-catch and a small delay between tries just in case two users hit it at the same time. I also used most of the code from here Link. The one thing I added was the call to get the current method name in the Catch.
Code:
Catch ex As Exception
  Logger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().Name & " Failed")
  Logger.LogInfo(ex)
  ...
That code also gets some detail on the actual error.

Auguy
Sylvania/Toledo Ohio
 
The [tt]ex.ToString[/tt] gives you a lot information about the error, including the line number and the source of the error. That should be enough to determine where the error happened.

"a small delay between tries just in case two users hit it at the same time" I would not worry about it much. I hope you do have pretty error-free code, so in this situation it is nearly impossible for more than one person to create an error and have problems writing into a text file. But that’s just my opinion. :)

Have fun.

---- Andy
 
Thanks again, I'm also going to log some of the more important actions taken by users and maybe some of the automatic procedures that are being run as well that are not errors. That's why I'm using the "GetCurrentMethod().Name". I'll probably send these to a different log file just to keep the error log clean.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top