Har*_*run 10 c# windows-services event-log
我使用以下代码在我的Windows服务应用程序中创建自定义事件日志:
public ServiceConstructor()
{
InitializeComponent();
if (!EventLog.SourceExists("WinService"))
{
EventLog.CreateEventSource("WinService", "WinServiceLog");
eventLog1.Source = "WinService";
eventLog1.Log = "WinServiceLog";
}
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Started");
}
Run Code Online (Sandbox Code Playgroud)
安装service.msi后,当我启动服务时,它启动然后停止.然后我在EventViewer Windows日志部分中发现以下错误:
服务无法启动.System.ArgumentException:在写入事件日志之前未设置Source属性.
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message)
at WinService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
如果源已存在,则看起来您没有初始化eventLog1.Source.
建议您将初始化代码移动到OnStart并从构造函数中移出.
并将这两行移出if语句:
eventLog1.Source = "WinService";
eventLog1.Log = "WinServiceLog";
Run Code Online (Sandbox Code Playgroud)
请尝试以下方法:
EventLog.CreateEventSource("WinService", "Application");
和
eventLog1.Log = "Application";
还在OnStart中添加以下内容:
eventLog1.Log="Application"
eventLog1.Source = "WinService";
| 归档时间: |
|
| 查看次数: |
17704 次 |
| 最近记录: |