简体   繁体   English

未在Windows服务中获取OnStart事件日志条目

[英]Not getting OnStart event log entry in Windows Service

I have the following code for a windows service project. 我有一个Windows服务项目的以下代码。 I have successfully built it and installed it. 我已成功构建并安装它。 When I start it I get an event started in the event log. 当我启动它时,我在事件日志中启动了一个事件。 However, I never get the event for "In Onstart" any idea why this is going on? 但是,我从来没有为“In Onstart”获得任何想法,为什么会这样?

namespace ADServiceCarlos
{
    public partial class ADServiceCarlos : ServiceBase
    {           
        public ADServiceCarlos()
        {
            InitializeComponent();
            this.AutoLog = true;

            if (!System.Diagnostics.EventLog.SourceExists("MySource")) 
            {         
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource","MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
        }

        protected override void OnStop()
        {
        }
    }
}

OK, so this may solve your problem. 好的,这可能会解决您的问题。 It's hard to tell exactly without being able to see all your code, but read this - more specifically the "caution" part. 如果没有能够看到你的所有代码,很难确切地说出来,但请阅读 - 更具体地说是“谨慎”部分。

Do not use the constructor to perform processing that should be in OnStart. 不要使用构造函数来执行应该在OnStart中的处理。 Use OnStart to handle all initialization of your service. 使用OnStart处理服务的所有初始化。 The constructor is called when the application's executable runs, not when the service runs. 构造函数在应用程序的可执行文件运行时调用,而不是在服务运行时调用。 The executable runs before OnStart. 可执行文件在OnStart之前运行。 When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. 例如,当您继续时,不会再次调用构造函数,因为SCM已将对象保存在内存中。 If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called. 如果OnStop释放在构造函数而不是OnStart中分配的资源,则第二次调用服务时将不会再次创建所需的资源。

So everything you are doing to initialise the event log in your constructor should be moved to the OnStart event. 因此,您在构造函数中初始化事件日志所做的一切都应该移到OnStart事件中。 This will ensure it is create properly each time the service start, which means you should be able to log your OnStart event correctly (providing you do it after initialisation) 这将确保每次服务启动时都能正确创建,这意味着您应该能够正确记录OnStart事件( OnStart您在初始化后执行此操作)

Based on what @musefan posted, here is an example. 基于@musefan发布的内容,这是一个例子。 I had to move everything out of the constructor completely. 我必须完全移出构造函数。

public class ServiceMain : System.ServiceProcess.ServiceBase
    {
        public const string SERVICE_NAME = "ServiceName";

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ServiceMain()
        {
            // This call is required by the Windows.Forms Component Designer.
            InitializeComponent();
        }

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = SERVICE_NAME;
            this.CanPauseAndContinue = true;
        }

        static void Main()
        {
            //Log all unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new ServiceMain() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            //Do your stuff here
        }

        static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
        {
            Environment.Exit(1);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM