Just want to ask,

how can i create a log file on vb.net,

what libraries should i need for this to work,.

thanks

Recommended Answers

All 10 Replies

If you just want a simple text file then you only require simple text output (make sure you open the log file in append mode). I have a suggestion that should be obvious but wasn't to some of our in-house developers. Make sure every line that you write to the log file begins with the date and time in the format

YYYY/MM/DD HH:MM:SS

or

YYYY-MM-DD HH:MM:SS

Where all fields are zero padded (eg use 03 for March instead of just 3) and the time is in 24 hour format. It makes it much easier to correlate events when you are troubleshooting and looking at logs from other apps (or system). One developer in particular would write out the date and time once in long format, then spew out 50-60 lines of information. It was difficult trying to find the start of each block and impossible to determine exactly when each piece of the block was generated. And trying to compare "September 12, 2007 9:53:17 PM" from his log to "2007-09-12 21:53:17" in another was not pleasant.

If you just want a simple text file then you only require simple text output (make sure you open the log file in append mode). I have a suggestion that should be obvious but wasn't to some of our in-house developers. Make sure every line that you write to the log file begins with the date and time in the format

YYYY/MM/DD HH:MM:SS

or

YYYY-MM-DD HH:MM:SS

Where all fields are zero padded (eg use 03 for March instead of just 3) and the time is in 24 hour format. It makes it much easier to correlate events when you are troubleshooting and looking at logs from other apps (or system). One developer in particular would write out the date and time once in long format, then spew out 50-60 lines of information. It was difficult trying to find the start of each block and impossible to determine exactly when each piece of the block was generated. And trying to compare "September 12, 2007 9:53:17 PM" from his log to "2007-09-12 21:53:17" in another was not pleasant.

I mean how can I create an error log file, for example, I want to logs all the ex.message from the try catch method.

An example of how to write to a log file is

Imports System.IO
Imports System.IO.File

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim filename As String = Application.StartupPath & "\test.log"
        Dim sw As StreamWriter = AppendText(filename)

        sw.WriteLine(Now() & " " & "sample log file entry")
        sw.Close()

    End Sub

End Class

The log file will be created if it doesn't exist. All of the applications that I wrote and maintained were written in vbScript. Because almost all of the required a logger, I created a logger class that I could include. A simplified example of using the logger class was

logger = New LoggerClass
logger.LogType = "daily"
logger.Open("myapp.log")

Anytime I wanted to write to the log file my apps would

logger.Write("any text")

The Write method would prepend YYYY-MM-DD HH:MM:SS to every line before writing. The LogType parameter was "daily", "monthly", "yearly" or blank. This would cause the creation of a new log file for every day, month or year (it would add the date to the name of the log file like "myapp 2012-01-09.log" as needed). Blank would just use one log file with no mod to the file name. You can get as fancy as you like but the basic method is

app start
open log file
log
log
log
.
.
.
close log file
app exit

An example of how to write to a log file is

Imports System.IO
Imports System.IO.File

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim filename As String = Application.StartupPath & "\test.log"
        Dim sw As StreamWriter = AppendText(filename)

        sw.WriteLine(Now() & " " & "sample log file entry")
        sw.Close()

    End Sub

End Class

The log file will be created if it doesn't exist. All of the applications that I wrote and maintained were written in vbScript. Because almost all of the required a logger, I created a logger class that I could include. A simplified example of using the logger class was

logger = New LoggerClass
logger.LogType = "daily"
logger.Open("myapp.log")

Anytime I wanted to write to the log file my apps would

logger.Write("any text")

The Write method would prepend YYYY-MM-DD HH:MM:SS to every line before writing. The LogType parameter was "daily", "monthly", "yearly" or blank. This would cause the creation of a new log file for every day, month or year (it would add the date to the name of the log file like "myapp 2012-01-09.log" as needed). Blank would just use one log file with no mod to the file name. You can get as fancy as you like but the basic method is

app start
open log file
log
log
log
.
.
.
close log file
app exit

I can't create this sample code.. even though i used the namespace system.IO and system.IO.file

the StreamWriter is still an error on the code.. could you please help me with this?

thanks

What version of Visual Studio are you using (I have VS 2010)? What is the specific error you get?

What version of Visual Studio are you using (I have VS 2010)? What is the specific error you get?

I am using VB express 2008,

the steamwriter occurs as an error that's why the site can't be run..

A method that should work is to use the scripting runtime library. In that case, make sure you add a reference (under the COM tab) to "Microsoft Scripting Runtime" (the actual file is c:\windows\system32\scrrun.dll), then use the following code

Public Class Form1

    Private fso As New Scripting.FileSystemObject

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim filename As String = Application.StartupPath & "\test.log"
        Dim ts As Scripting.TextStream = fso.OpenTextFile(filename, Scripting.IOMode.ForAppending, True)

        ts.WriteLine(Now() & " " & "sample log file entry")
        ts.Close()

    End Sub

End Class

A method that should work is to use the scripting runtime library. In that case, make sure you add a reference (under the COM tab) to "Microsoft Scripting Runtime" (the actual file is c:\windows\system32\scrrun.dll), then use the following code

Public Class Form1

    Private fso As New Scripting.FileSystemObject

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim filename As String = Application.StartupPath & "\test.log"
        Dim ts As Scripting.TextStream = fso.OpenTextFile(filename, Scripting.IOMode.ForAppending, True)

        ts.WriteLine(Now() & " " & "sample log file entry")
        ts.Close()

    End Sub

End Class

Okay i have used this syntax and now it's error is the Application.StartupPath,
how can I resolved this?

Thanks

That is apparently another feature your version does not support. Just replace it with the actual path where you want to save the log file.

That is apparently another feature your version does not support. Just replace it with the actual path where you want to save the log file.

yup, already replaced it.. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.