title: Windows service unable write into event log
body: <p>I have created the following report service but I am unable to log any thing in the EventLog. In the project settings I have set App Type as windows Service and Startup project as SUB Main... Can any one please suggest whats wrong with the code</p>
  
  <p>Program.vb</p>
  
  <pre><code>Imports System.ServiceProcess
  
      Namespace ReportingService
  
      Public Class Program
          Private Sub New()
          End Sub
          ''' &lt;summary&gt;The main entry point for the application.&lt;/summary&gt;
          Private Shared Sub Main(args As String())
              Dim ServicesToRun As ServiceBase()
              ServicesToRun = New ServiceBase() {
                  New ReportingImmediate()
              }
              ServiceBase.Run(ServicesToRun)
          End Sub
      End Class
  End Namespace
  </code></pre>
  
  <p>ReportService.vb</p>
  
  <pre><code>Imports System.ServiceProcess
  Imports System.Timers
  Imports System.Configuration
  
  Public Class ReportingImmediate
      Inherits ServiceBase
  
  #Region "Members"
  
      Private ReadOnly time As Timer
      Private ReadOnly rptHelper As ReportingHelper
      Private ReadOnly timeInterval As String
  
  #End Region
  
      Public Sub New()
          ' Initialize Logs
          InitializeComponent()
          ' Initialize other components
          time = New Timer()
          timeInterval = ConfigurationManager.AppSettings("ImmediateReRunInterval") '10000
          time.Interval = Integer.Parse(timeInterval)
          AddHandler time.Elapsed, AddressOf TimeElapsed
  
          rptHelper = New ReportingHelper()
      End Sub
  
  #Region "Timer Event"
  
      ''' &lt;summary&gt;time Elapsed&lt;/summary&gt;
      ''' &lt;param name="sender"&gt;The object that raised the event sender&lt;/param&gt;
      ''' &lt;param name="e"&gt;Event data passed to the handler e&lt;/param&gt;
      Private Sub TimeElapsed(sender As Object, e As ElapsedEventArgs)
          time.Enabled = False
  
          rptHelper.WriteToEventLog("Logging Report Service")
  
          ' Enable the Timer
          time.Enabled = True
          time.Interval = Integer.Parse(timeInterval)
      End Sub
  
  #End Region
  
  #Region "Service Events"
  
      ''' &lt;summary&gt;On Start&lt;/summary&gt;
      ''' &lt;param name="args"&gt;Arguments&lt;/param&gt;
      Protected Overrides Sub OnStart(args As String())
          time.Enabled = True
      End Sub
  
      ''' &lt;summary&gt;On Stop&lt;/summary&gt;
      Protected Overrides Sub OnStop()
          time.Enabled = False
      End Sub
  
  #End Region
  
  End Class
  </code></pre>
  
  <p>ReportHelper.vb</p>
  
  <pre><code>    Imports System.Data.OleDb
      Imports System.Configuration
      Imports System.Threading.Tasks
      Imports System.IO
      Imports System.IO.Packaging
      Imports System.Text
      Imports System.Net.Mail
      Imports System.Net
  
      Public Class ReportingHelper
  
          Public Function WriteToEventLog(ByVal entry As String, Optional ByVal appName As String = "ReportingService", Optional ByVal eventType As  _
                              EventLogEntryType = EventLogEntryType.Information, Optional ByVal logName As String = "RepotingServiceImmediate") As Boolean
  
          Dim objEventLog As New EventLog
  
          Try
  
              'Register the Application as an Event Source
              If Not EventLog.SourceExists(appName) Then
                  EventLog.CreateEventSource(appName, logName)
              End If
  
              'log the entry
              objEventLog.Source = appName
              objEventLog.WriteEntry(entry, eventType)
  
              Return True
  
          Catch Ex As Exception
  
              Return False
  
          End Try
  
      End Function
  End Class
  </code></pre>