当我尝试启动该服务时,我收到以下错误:
服务无法启动.System.ArgumentException:源'Bar source'未在日志'Bar2'中注册.(它在日志'Bar source'中注册.)"Source和Log属性必须匹配,或者您可以将Log设置为空字符串,它将自动匹配Source属性.在System.Diagnostics.EventLogInternal.在Bar.Service1的System.Diagnostics.EventLog.WriteEntry(String message)上的System.Diagnostics.EventLogInternal.WriteEntry(String message,EventLogEntryType type,Int32 eventID,Int16 category,Byte [] rawData)中的VerifyAndCreateSource(String sourceName,String currentMachineName) C:\ Program Files(x86)\ Bar中的.writeToLog(String msg) - 用于AP:\ Service1.vb:C:\ Program Files(x86)中Bar.Service1.OnStart(String [] args)的第292行栏 - 用于APPS\Service1.vb:第37行,System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)
有谁知道什么会导致这个问题?我Bar2在代码中没有提到,程序文件中的文件夹被称为"Bar2",但我将其更改为"Bar".
请指教!
这是WriteToLog功能:
Private Sub writeToLog(ByVal msg As String)
Dim evtLog As New EventLog
If Not Diagnostics.EventLog.SourceExists("Bar") Then
Diagnostics.EventLog.CreateEventSource("Bar", "Log of Bar")
End If
evtLog.Source = "Bar"
evtLog.Log = "Log of Bar"
evtLog.WriteEntry(msg)
End Sub
Run Code Online (Sandbox Code Playgroud)
Log属性存储事件日志的名称,如控制面板>管理工具>事件查看器小程序中所示.大多数应用程序都会记录到Windows日志>应用程序日志,这是默认情况下您不分配Log属性.但您可以为自己的应用程序创建自定义日志,在"应用程序和服务日志"条目下可见.
首次使用CreateEventSource创建日志时,此信息将记录在注册表中.Log属性将来必须匹配,如果不匹配则会出现此异常.所以你知道的是事实上已经存在日志,但是你曾经用不同的日志名称"Bar2"创建它.可能决定这不是一个非常好的名称并更改Log属性.KABOOM.
将Log属性设置为空字符串不是一种解决方法.它将继续记录到原始日志名称,使用注册表返回其名称.您可以使用Regedit.exe修复不需要的注册,导航到HKLM\SYSTEM\CurrentControlSet\Services\EventLog.如果删除该条目,则还应删除相应的.evtx文件,File值将为其提供其路径名.
Leaving the log as an empty string solved the error:
Private Sub writeToLog(ByVal msg As String)
Dim evtLog As New EventLog
If Not Diagnostics.EventLog.SourceExists("Bar") Then
Diagnostics.EventLog.CreateEventSource("Bar", "")
End If
evtLog.Source = "Bar"
evtLog.Log = ""
evtLog.WriteEntry(msg)
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1392 次 |
| 最近记录: |