Windows Service and StreamWriter

G

Guest

I am trying to create a windows service. The part I am having trouble with
is writing text to a log file. I am using a very basic StreamWriter function
to try to test this.

I have created a very basic service app to try to test this:

OnStart
timer1.enabled = True

OnStop
Timer1.enabled = Fales

Private Sub Timer1.elapsed (ByVal...blah blah blah) Handles blah

Dim fw as new StreamWriter("C:\LogFile", True)
fw.WriteLine("This is a test from the service")

End sub

but, of course, the text file is blank. Please help.

Also, what is the best way to debug a service. Is there a way to step
through procedures?

Thank you

MATT
 
J

Jared Parsons [MSFT]

Hello MATT,
I am trying to create a windows service. The part I am having trouble
with is writing text to a log file. I am using a very basic
StreamWriter function to try to test this.

I have created a very basic service app to try to test this:

OnStart
timer1.enabled = True
OnStop
Timer1.enabled = Fales
Private Sub Timer1.elapsed (ByVal...blah blah blah) Handles blah

Dim fw as new StreamWriter("C:\LogFile", True)
fw.WriteLine("This is a test from the service")
End sub

but, of course, the text file is blank. Please help.

The problem is that StreamWriter buffers it's data in memory. You have to
force it to push the data onto disk by calling the Close method. A better
way is to use the "Using" statement which will force a call to Close (via
IDisposable) even in the presence of an exception

Using ( fw As New StreamWriter("C:\LogFile", True))
....
End Using
 
B

Brian Gideon

Matt,

The best way to debug a service is to allow it to run as a console or
windows applications. There are a few ways of doing this. The
simplest might look like this.

Shared Sub Main()

If Environment.UserInteractive Then

' Run as a console application.
Dim service As Service1 = New Service1
service.OnStart(Nothing)
Console.WriteLine("Press ENTER to quit...")
Console.ReadLine()
service.OnStop()

Else

' Assume the Service Control Manager invoked the application.
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End If

End Sub

There are some caveats about using this method, but for the most part
it works pretty well. The advantage here is that you can put a
breakpoint in the Main method and it will hit when launched from the
debugger. If you really want the application to run as a service
during a debugging session then you'll have to start it via the SCM and
attach the debugger manually. That can be done by clicking Debug |
Processes in Visual Studio. The downside to that is that you will miss
the entry point of the application.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top