【发布时间】:2013-03-25 03:36:14
【问题描述】:
我有一个 Windows 服务。在我为其添加代码以开始记录之前,它工作正常。现在,当我尝试启动服务时,我收到以下错误:
本地计算机上的 GBBService 服务启动然后停止。一些 如果没有工作要做,服务会自动停止,因为 例如,性能日志和警报服务
这是我的服务的代码: - 从项目安装程序内部
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
string eventSource = "GBBServiceLog";
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Source = eventSource;
installer.Log = "My GBBServiceLog";
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
return eventLogInstaller;
}
return null;
}
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
// Start the service after installation
using (ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName))
{
sc.Start();
}
}
}
在我的服务范围内:
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBB Service Log";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("GBBService Service Started");
}
我的代码做错了吗?
【问题讨论】:
-
是不是因为您在安装程序和服务中创建了事件日志
My GBBServiceLog,所以您想写信给My GBB Service Log -
所以两个地方都需要相同吗?这是我在处理它时引用的 url:blog.embrodesign.com/2012/05/event-log-from-windows-service 不确定日志名称是相同还是不同
-
我很确定您需要相同的名称,
My GBB Service Log不存在,因此您需要在服务中创建它或使用您在安装程序中创建的名称。 -
@sa_ddam213 就是这样,请将其添加为答案,以便我可以关闭它!
-
没问题 :),很高兴你成功了 :)
标签: c# logging windows-services event-log custom-eventlog