Locked Created a Windows service and can't log into event log

  • Friday, June 29, 2012 3:32 AM
     
      Has Code

    I followed the example at the following to create a windows service solution

    http://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.100%29.aspx

    http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled%28v=vs.80%29.aspx

    http://stackoverflow.com/questions/8754069/how-to-create-a-custom-event-log-using-c-sharp

    The event log doesn't get updated with the eventLog1.WriteEntry("<Some Text>"); calls. The following it the source

    public CService()
            {
                InitializeComponent();
     
                http://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.100%29.aspx
     
                this.ServiceName = ConfigurationManager.AppSettings.Get("CService");
                string sourceName = ConfigurationManager.AppSettings.Get("CServiceSource");
                string logName = ConfigurationManager.AppSettings["CServiceLog"];
     
                if (!EventLog.SourceExists(sourceName))
                {
                    EventLog.CreateEventSource(sourceName, logName);
                }
            }
     
            protected override void OnStart(string[] args)
            {
                string sourceName = ConfigurationManager.AppSettings.Get("CServiceSource");
                string logName = ConfigurationManager.AppSettings["CServiceLog"];
                eventLog1.Source = sourceName; // "CTSMonAgent";
                eventLog1.Log = logName;
                eventLog1.WriteEntry("In OnStart.");
                /* Notes by Paul
                   A service application is designed to be long running. As such, it usually polls or monitors something in the system. The monitoring is set up in the OnStart method. However, OnStart does not actually do the monitoring. The OnStart method must return to the operating system once the service's operation has begun. It must not loop forever or block. To set up a simple polling mechanism, you can use the 
                 
                 * System.Timers.Timer 
                 http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.80%29.aspx
                 
                 component. In the OnStart method, you would set parameters on the component, 
                 
                 * and then you would set the Enabled property to true. 
                 http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled%28v=vs.80%29.aspx
                 
                 The timer would then raise events in your code periodically, at which time your service could do its monitoring.
                 */
     
                // Normally, the timer is declared at the class level, so
                // that it doesn't go out of scope when the method ends.
                // In this example, the timer is needed only while Main 
                // is executing. However, KeepAlive must be used at the
                // end of Main, to prevent the JIT compiler from allowing 
                // aggressive garbage collection to occur before Main 
                // ends.
                System.Timers.Timer aTimer = new System.Timers.Timer();
     
                // Hook up the Elapsed event for the timer.
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
     
                // Set the Interval to 2 seconds (2000 milliseconds).
                aTimer.Interval = 200000; //5 minutes 2000;
                aTimer.Enabled = true;
     
                //Console.WriteLine("Press the Enter key to exit the program.");
                //Console.ReadLine();
     
                // Keep the timer alive until the end of Main.
                GC.KeepAlive(aTimer);
            }
     
            // Specify what you want to happen when the Elapsed event is 
            // raised.
            private static void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                //Console.WriteLine("Hello World!");
                string sourceName = ConfigurationManager.AppSettings.Get("CServiceSource");
                string logName = ConfigurationManager.AppSettings["CServiceLog"];
                //if (!EventLog.SourceExists(sourceName))
                //{
                //    EventLog.CreateEventSource(sourceName, logName);
                //}
                EventLog eventLog1 = new EventLog();
                eventLog1.Source = sourceName; // "CTSMonAgent";
                eventLog1.Log = logName;
                eventLog1.WriteEntry("In OnTimedEvent.");
     
            }
     
            protected override void OnStop()
            {
                eventLog1.WriteEntry("In OnStop.");
            }

All Replies

  • Friday, June 29, 2012 11:26 AM
     
      Has Code

    Are you sure, you have a valid SourceName and Logname in configuration.

    If possible make sure your sourcename and logname is not empty or null on below code snipet.

    eventLog1.Source = sourceName; // "CTSMonAgent";
    eventLog1.Log = logName;
    Restarting the system helps few people it seems.


    Lingaraj Mishra

  • Friday, June 29, 2012 12:33 PM
    Moderator
     
     Answered Has Code

    Hi paul,

    Welcome to the MSDN Forum.

    I agree with Lingaraj. Please try to follow this suggestion.

    In addition, would you like to add this code snippet before write the log:

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

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked As Answer by PaulDAndrea Friday, June 29, 2012 7:05 PM
    •  
  • Friday, June 29, 2012 3:55 PM
     
     

    Actually the problem is that I was looking in 'Windows Logs' on Windows 7 and not in 'Applications and Services Logs'.  Now I need to figure out why the OnTimedEvent kicks off only once.

  • Friday, June 29, 2012 5:13 PM
     
     

    I reverted the code back to where I had it in my first post and uninstalled.  The I installed again and it would not install because the service was marked for deletion.  I rebooted and then installed it successfully; now I get the following error when trying to start:

    Windows could not start the CTSMonAgent service on Local Computer

    Error 1053: The service did not respond to the start or control request in a timely fashion.

    I've tried Sytem Restore to yesturday and everything I find through internet Bing and Google search.  Nothing seems to work.

    Please help me restore this so I can start the service again without the error.

  • Friday, June 29, 2012 7:00 PM
     
     

    OK, I have it working now and This case if fixed

    I'm new at creating a service; but that is not a good excuse for doing the dumb mistakes I made.

  • Saturday, June 30, 2012 9:22 AM
    Moderator
     
     

    Hi Paul,

    I am glad to hear your issue is gone.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.