【发布时间】:2012-11-23 00:38:38
【问题描述】:
我开发了一个 32 位服务,我在 Windows 7 Home Premium x64 中运行它。 问题是当我启动它时,windows 会给我以下消息
本地计算机上的 WLConsumer 服务启动然后停止。部分服务停止 如果它们未被其他服务或程序使用,则自动进行。
我在事件日志中发现以下消息
服务无法启动。 System.ArgumentException:日志 WLConsumer 已注册为本地计算机上的源。 在 System.Diagnostics.EventLogInternal.CreateEventSource(EventSourceCreationData sourceData) 在 System.Diagnostics.EventLogInternal.VerifyAndCreateSource(字符串 sourceName,字符串 currentMachineName) 在 System.Diagnostics.EventLogInternal.WriteEntry(字符串消息,EventLogEntryType 类型,Int32 eventID,Int16 类别,Byte[] rawData) 在 System.Diagnostics.EventLog.WriteEntry(字符串消息,EventLogEntryType 类型) 在 C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs: 136 行中的 WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) 在 C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 63 中的 WeblogicConsumerService.WeblogicConsumer.OnStart(String[] args) 在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)
这是我在 OnStart() 方法中的代码块
protected override void OnStart(string[] args)
{
#region WEBLOGIC CREDENTIALS
try
{
//Weblogic URL
this.url = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("URL").ToString();
//Queue name
this.qName = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("Queue").ToString();
//Weblogic login name
this.user = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("User").ToString();
//Weblogic password
this.pwd = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("Pwd").ToString();
//Weblogic Connection Factory
this.cfName = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("ConnectionFactory").ToString();
//root folder
this.rFolder = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("root").ToString();
}
catch (Exception e)
{
winEventlogMe(e.Message, "WLRegistryKeys", 'e');
}
#endregion
winEventlogMe("Successful start", "SeriviceStartup", 'i');
synchro.Enabled = true;
}
winEventLogMe 是我调用的日志记录方法。
public static void winEventlogMe(string logTxt, string logSrc, char entryType)
{
#region Log
//Log to event log
EventLog theEvent = new EventLog("WLConsumer");
theEvent.Source = logSrc;
if (entryType == 'e')
theEvent.WriteEntry(logTxt, EventLogEntryType.Error);
else if (entryType == 'i')
theEvent.WriteEntry(logTxt, EventLogEntryType.Information);
else if (entryType == 'w')
theEvent.WriteEntry(logTxt, EventLogEntryType.Warning);
else
theEvent.WriteEntry(logTxt, EventLogEntryType.Error);*/
#endregion
}
当我在 OnStart() 方法中注释掉对 winEventLogMe() 方法的调用时,服务启动时不会出现错误。所以显然 winEventLogMe() 方法有问题。 有人可以帮我找出问题所在,因为我现在完全不知道如何解决这个问题。
提前感谢 :)
@nick_w 我已按照您的建议编辑了我的代码,服务已成功启动。但在停止它时,我收到以下消息:
未能停止服务。 System.ArgumentException:源“WLConsumer2012”未在日志“ServiceStop”中注册。 (注册在日志'ServiceStartup'中。) " Source 和 Log 属性必须匹配,或者您可以将 Log 设置为空字符串,它会自动匹配 Source 属性。 在 System.Diagnostics.EventLogInternal.VerifyAndCreateSource(字符串 sourceName,字符串 currentMachineName) 在 System.Diagnostics.EventLogInternal.WriteEntry(字符串消息,EventLogEntryType 类型,Int32 eventID,Int16 类别,Byte[] rawData) 在 System.Diagnostics.EventLog.WriteEntry(字符串消息,EventLogEntryType 类型) 在 C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs: 139 行中的 WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) 在 C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 70 中的 WeblogicConsumerService.WeblogicConsumer.OnStop() 在 System.ServiceProcess.ServiceBase.DeferredStop()
这里是 OnStop() 方法
protected override void OnStop()
{
winEventlogMe("Successful stop", "ServiceStop", 'i');
}
这些事件日志开始让我很困惑。我做过同样的方法登录其他服务,从来没有遇到过这样的问题。我怎么能在这个服务中得到这些错误,但它与我所做的所有其他错误并没有太大区别:(
【问题讨论】:
-
如果您仍在解决这个问题,请查看我的最新编辑。