Vb.net Windows服务无法写入事件日志

Vb.net Windows服务无法写入事件日志,vb.net,windows-services,Vb.net,Windows Services,我已经创建了以下报表服务,但无法在事件日志中记录任何内容。在项目设置中,我已将应用程序类型设置为windows服务,并将启动项目设置为SUB-Main。。。有人能告诉我代码有什么问题吗 Program.vb Imports System.ServiceProcess Namespace ReportingService Public Class Program Private Sub New() End Sub ''' <

我已经创建了以下报表服务,但无法在事件日志中记录任何内容。在项目设置中,我已将应用程序类型设置为windows服务,并将启动项目设置为SUB-Main。。。有人能告诉我代码有什么问题吗

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

您的服务将无法
EventLog.CreateEventSource
,因为它不是以管理员身份运行的(参考:的备注部分中的注释):

要在Windows Vista及更高版本或Windows Server 2003中创建事件源,您必须具有管理权限

此要求的原因是必须搜索所有事件日志(包括安全日志),以确定事件源是否唯一。从Windows Vista开始,用户没有访问安全日志的权限;因此,会引发SecurityException

由于您不应以管理员身份运行服务,因此解决方案是编写一个小程序,您可以使用管理员权限运行该程序来创建事件源


DWPReportingHelper.WriteToEventLog
中的
Try…Catch
可能对您隐藏了一个错误。

因为您还需要管理员权限才能安装服务,同时创建事件源可能是明智的。我正在使用管理员权限安装服务,但事件日志中唯一的条目是服务成功启动和停止,我没有编写它。我尝试在ReportService.VB文件的new和timer事件中直接创建日志,但没有任何结果…@NikhilGupta尝试使用现有日志,例如使用
logName=“Application”
。我使用以下Windows注册表编辑器版本5.00[HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\App\U Name]解决了此问题作为附录,您不需要是管理员就可以写入Windows事件日志,但需要授予您使用wevtutil编辑ACL(访问控制列表)的权限。如果你决定不让所有的东西都以管理员的身份运行(实际上你不应该总体上这样做),那么你可以引导你朝着正确的方向前进。
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
    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