Writing to the Event Log vb net

Hi,  I'm trying to write to the event log in a Windows Service Application I'm developing.

The Service builds OK. But the service just stops as soon as it starts without any logging.

Any idea?

    If Not System.Diagnostics.EventLog.SourceExists("MyService") Then
      System.Diagnostics.EventLog.CreateEventSource("NyService", "MyService Log")
    End If

    _eventLog = New EventLog()
    _eventLog.Source = "MyService"
    _eventLog.WriteEntry(" Service started.", EventLogEntryType.Information)
Who is Participating?
CodeCruiserConnect With a Mentor Commented:
You can create event source as part of the setup which has to be run as admin and then just log events in the service.
Meir RivkinFull stack Software EngineerCommented:
is this code inside OnStart() function in your window service class?
do u have exception handling on your code to detect errors?
you can also use Debugger.Break() inside the class in order to debug the code when the service starts.
jmsjmsConnect With a Mentor Author Commented:
Couldnt get it to work.

Looks like an application needs to have admin rights to use the CreateEventSource function and the service I have created hasnt got it.

If anyone has sample code to use Event logs in Services that actually works without running the server as admin please let me know!
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

jmsjmsAuthor Commented:
Sounds good!  Can this be done with the LE version of install shield?
Not sure. You can create another exe which you can ask user to launch as admin when you find out, in your main program, that event source does not exist.
jmsjmsAuthor Commented:
But you cant have any user-interaction with a service so how could you do this?
There are work arounds but i think first thing to do is to determine if installshield LE can help you.
jmsjmsAuthor Commented:
I've run out of time to devote to this, so cant confirm whether installsheild LE can help here of not.

I'll mark this as an answer so people can see what I found out and one possible way forward.
jmsjmsAuthor Commented:
My comment indicates that just getting a service to create the event log source will not work (despite some examples on the web) UNLESS the service has admin rights.

Codecruisers suggestion that it's done as part of the installation is a good idea but I can't confirm this works yet.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.