Home All Groups Group Topic Archive Search About

Windows Service and StreamWriter

Author
3 Jul 2006 10:18 AM
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.

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

Thank you

MATT

Author
3 Jul 2006 3:53 PM
Jared Parsons [MSFT]
Hello MATT,

Show quoteHide quote
> 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

--
Jared Parsons [MSFT]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
3 Jul 2006 8:47 PM
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


MATT wrote:
Show quoteHide quote
> Also, what is the best way to debug a service.  Is there a way to step
> through procedures?
>