Writing a Log file

Erdenemandal

Well-known member
Joined
Jul 5, 2004
Messages
63
Location
Deutschland
Hi , All

I have a problem . I want to save every operation into Log file. I have "Start", "working" form.

But strange thing is from "Start" form it writes into Log file and from "Working" form it does not write anything,

Can somebody help me-

this is a code
in module WriteLog function declared PUBLIC

Code:
 Public Sub WriteLog(ByVal text1 As String, ByVal Text2 As String)

                If Not System.IO.Directory.Exists(CurDir() + "\sources\Logs\") Then
                        System.IO.Directory.CreateDirectory(CurDir() + "\sources\Logs\")

                End If

                Dim myLogName As String = CurDir() + "\sources\Logs\Running.log"

                If System.IO.File.Exists(myLogName) Then
                        Dim FI As New System.IO.FileInfo(myLogName)

                        If FI.Length > 100000 Then
                                Dim dS As String = Date.Now.Today.ToShortDateString
                                dS = dS.Replace(".", "")
                                Dim dT As String = DateTime.Now.ToLongTimeString
                                dT = dT.Replace(":", "")
                                System.IO.File.Move(myLogName, CurDir() + "\sources\Logs\Log_" + dS + dT + ".bak")
                        End If
                End If

                Try
                        Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myLogName, True)

                        sw.WriteLine(Date.Now.Today.ToShortDateString + " " + DateTime.Now.ToLongTimeString + " : " + text1 + ControlChars.Tab + ControlChars.Tab + Text2)

                        sw.Close()

                Catch ex As Exception
                        MsgBox(ex.Message)
                End Try
        End Sub

in Start form
Code:
 WriteLog("Patient " & mPersonID & " has Logged in ", "DONE")

in Working form

Code:
WriteLog("Basci stimulation Started", "Done")
 
solution

At the end I found a problem . Because of "Curdir" function . It gives me a current directory. But In "Working" form I use openFileDiaglog. It changes current folder name . So that after "working" form "Curdir" retunrs me openned folder name.

Therefore Please be careful use "CurDir" after FileOPenDialogBox

Thanks,
Erdenemandal
 
You may be better of using the .Net version of CurDir anyway
Code:
System.IO.Directory.GetCurrentDirectory()
or
Environment.CurrentDirectory
If the folders are always going to be relative to the running applications path then
Code:
Application.ExecutablePath
maybe a better choice.
 
What also helps is setting the RestoreDirectory property of the file dialog. It ensures that the current directory, after the dialog is closed, is restored to the location it was pointing before the dialog was opened.
 
Back
Top