如何将服务附加到事件查看器 [英] How to attach service to event viewer

查看:66
本文介绍了如何将服务附加到事件查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个exe和一个Windows服务,我会一直运行Windows服务。但是,我想确保在启动exe时启动服务。所以,我将在活动查看器中输入启动应用程序的条目,在此条目中我想启动Windows服务。



我尝试了什么:



我试过下面的代码来启动另一个exe。但是,我想启动Windows服务

I have an exe and a windows service, I will keep windows service running all the time. But, I want to make sure that service is started if exe is launched. So, I will make an entry to event viewer that application is started, upon this entry I want to start windows service.

What I have tried:

I have tried below code to start another exe. But, I want to start windows service

private static void CreateEventScheduler()
        {
            using (TaskService ts = new TaskService())
            {
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Auto - Start Service";
                EventTrigger eTrigger = (EventTrigger)td.Triggers.Add(new EventTrigger());
                eTrigger.SetBasic("Application", "TestApplication", 12345);
                eTrigger.Enabled = true;
                //eTrigger.ExecutionTimeLimit = TimeSpan.Zero;
                td.Actions.Add(@"D:\Application\Demo Applications\Console\AppForScheduler\AppForScheduler\bin\Debug\AppForScheduler.exe");
                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition(TaskName, td);
            }
        }

推荐答案

阅读本文:构建Windows事件日志观察器服务流程以将事件日志条目导出为RSS源 [ ^ ]



你会发现它有你正在寻找的代码示例。
Have a read of this article: Build Windows Event Log Watcher Service Process to Export Event Log Entries as RSS Feed[^]

You will find that it has the code sample that you are looking for.


好吧,我手动创建了任务调度程序, Windows App记录系统事件消息时会触发此调度程序。即在调度程序的触发器选项卡中,从开始任务下拉列表中选择在事件上,添加源名称,添加事件ID。通过App,Source和EventID记录消息时应与任务计划程序中提到的相同。



在操作部分,选择启动程序选项,在程序/脚本文本框中添加%windir%/ system32 / sc.exe,在添加参数文本框中添加 启动Windows服务名称



创建任务后,右键单击任务>>导出,这将创建一个xml。



在INNO SETUP中添加此XML作为部署包的一部分,这将在安装应用程序时将事件注册到机器。



此外,在Windows应用程序OnStart事件上添加了事件日志。



因此,每当应用程序启动时,OnStart事件被触发,Log将被注册到系统事件日志,具有特定的ID和Source。输入日志后,将触发相关的调度程序,这将启动Windows服务



以下是通过OnStart事件向事件查看器发送loggin消息的c#代码



Well, I created task scheduler manually, this scheduler gets triggered when Windows App logs system event message. I.e. In Trigger tab of scheduler, Select "On an event" from "Begin the task" drop down, add source name, Add EventID. While logging a message through App, Source and EventID should be same as mentioned in the task scheduler.

In Action section, select "Start a program" option, in program/script textbox add "%windir%/system32/sc.exe", in "Add argument" textbox add start "Windows Service Name".

Once task created, right click task >> export, this will create an xml.

Added this XML in INNO SETUP as a part of deployment package, this will register event to the machine whenever application is installed.

Also, added event log on windows application OnStart event.

So, whenever application is started, OnStart event is fired, Log will be registered to System Event Log, with specific ID and Source. Once the log is entered, associated scheduler will be triggered, which will start Windows service

Below is c# code for loggin message to event viewer through OnStart event

//Add reference to System.Diagnostics namespace
protected override void OnStartup(StartupEventArgs e)
{
  const string Source = "TestWindowsApp", Log = "Application", Event = "TestWindowsApp       Application Started";
  const int EventID = 16317;

if (!EventLog.SourceExists(Source))
   EventLog.CreateEventSource(Source, Log);
   EventLog.WriteEntry(Source, String.Format("{0}: {1}", Event, System.DateTime.Now),     EventLogEntryType.Information, EventID);
}


这篇关于如何将服务附加到事件查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆