2014-09-14 2 views
0

다음 보고서 서비스를 만들었지 만 EventLog에 아무 것도 기록 할 수 없습니다. 나는 창문 서비스 및 SUB 홈페이지로 시작 프로젝트로 응용 프로그램 유형을 설정 한 프로젝트 설정에서 ... 하나는 코드Windows 서비스에서 이벤트 로그에 쓸 수 없음

Program.vb

Imports System.ServiceProcess 

    Namespace ReportingService 

    Public Class Program 
     Private Sub New() 
     End Sub 
     ''' <summary>The main entry point for the application.</summary> 
     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 

ReportService.vb

에 무슨 잘못 제안시겠습니까
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" 

    ''' <summary>time Elapsed</summary> 
    ''' <param name="sender">The object that raised the event sender</param> 
    ''' <param name="e">Event data passed to the handler e</param> 
    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" 

    ''' <summary>On Start</summary> 
    ''' <param name="args">Arguments</param> 
    Protected Overrides Sub OnStart(args As String()) 
     time.Enabled = True 
    End Sub 

    ''' <summary>On Stop</summary> 
    Protected Overrides Sub OnStop() 
     time.Enabled = False 
    End Sub 

#End Region 

End Class 

ReportHelper.vb 것은

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 

답변

2

서비스는로 할 수 없습니다 이 관리자로 (: EventLog.CreateEventSource Method의 설명 부분에서 주 심판) : 실행하지 않기 때문에은

Windows Vista에서 이벤트 소스를 만들려면 나중에 또는 윈도우 서버 2003 관리자 권한이 있어야합니다.

이 요구 사항의 이유는 보안을 비롯한 모든 이벤트 로그를 검색하여 이벤트 원본이 고유한지 여부를 확인해야하기 때문입니다. Windows Vista부터 보안 로그에 액세스 할 수있는 권한이 없습니다. 따라서 SecurityException이 발생합니다.

관리자로 서비스를 실행하면 안되므로 관리자 권한으로 실행하여 이벤트 소스를 만드는 작은 프로그램을 작성하는 것이 해결책입니다.

Try...Catch (DWPReportingHelper.WriteToEventLog)은 오류를 숨기고 있습니다.

+0

서비스를 설치하려면 관리자 권한이 필요하므로 동시에 이벤트 소스를 만드는 것이 좋습니다. –

+0

관리자 권한을 사용하여 서비스를 설치하고 있지만 이벤트 로그의 유일한 항목은 서비스가 성공적으로 시작되었고 성공적으로 중지되었으며 작성하지 않았습니다. ReportService.VB 파일의 새 이벤트와 타이머 이벤트를 직접 작성하려고 시도했지만 아무 일도 일어나지 않습니다. –

+0

@NikhilGupta 기존 로그를 사용하십시오. 'logName = "Application"'을 사용하십시오. –

관련 문제