【问题标题】:Not getting OnStart event log entry in Windows Service未在 Windows 服务中获取 OnStart 事件日志条目
【发布时间】:2012-10-09 11:51:28
【问题描述】:

我有以下代码用于 Windows 服务项目。我已经成功构建并安装了它。当我启动它时,我会在事件日志中启动一个事件。但是,我从来没有得到“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()
        {
        }
    }
}

【问题讨论】:

  • 从 EventViewer 清除事件日志后尝试...

标签: c# windows service


【解决方案1】:

好的,这可能会解决您的问题。如果无法查看所有代码,很难准确判断,但read this - 更具体地说是“警告”部分。

不要使用构造函数来执行应该在 开机。使用 OnStart 处理服务的所有初始化。这 构造函数在应用程序的可执行文件运行时调用,而不是在 服务运行。可执行文件在 OnStart 之前运行。当你 继续,例如,构造函数不会再次调用,因为 SCM 已经将对象保存在内存中。如果 OnStop 释放资源 在构造函数中而不是在 OnStart 中分配,所需的 第二次服务时不会再次创建资源 调用。

因此,您在构造函数中初始化事件日志的所有操作都应移至OnStart 事件。这将确保每次服务启动时都能正确创建它,这意味着您应该能够正确记录您的 OnStart 事件(前提是您在初始化后执行此操作)

【讨论】:

  • 试过了,但是没有用。我查看了我的代码,它看起来像是我在网上看到的几个示例,包括 microsoft 网站上的示例。
  • 我想出了那部分,但现在,我在我的服务上使用了一个计时器,但我从来没有参加过票务活动。我的 OnStart 中有 timer1.Enabled = true 和 timer1.Start(),在我的 timer1_Tick 中我只有另一个事件条目,但从未获得该条目。
  • @user541597:您的计时器问题需要作为一个新问题解决
  • @user541597 您可以为遇到相同问题的其他用户添加您的答案。
【解决方案2】:

根据@musefan 发布的内容,这是一个示例。我必须将所有内容完全移出构造函数。

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);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多