Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Logging Shutdown event in Windows Service

Logging Shutdown event in Windows Service

Scheduled Pinned Locked Moved C#
helpdatabasesql-serversysadmintools
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abupsman
    wrote on last edited by
    #1

    I would like to request some suggestions on a problem I am having with a Windows Service I built. It installs and runs fine, using a timer every minute to check for files, then running a process. I have the process logging to a Windows Event Log, also, every time if does find and process a file it writes a record to a remote SQL server with file stats. The third thing the service does is send out e-mails. One type is a daily summary with totals and stats The second is whenever an error is caught, it collects the error data and sends it to a support group (Also writes to the event log) I also e-mail, write SQL & log at service Start and Stop. The issue is that the logging works OK at Stop if I do it from the Services panel in Admin Tools. However, if the Box is re-started by doing a Start/Shutdown/Re-start, I get no logging or e-mails to notify that the box is going down. I do get the Start up messages and logging minutes later when the system restarts. However, if someone were to do a Start/Shutdown/Shutdown, I would not be notified and it might be several hours before other processes noticed that the data stream had stopped and complain about it. Here is the code from my OnStop section: protected override void OnStop() { //Close all open connections closeResources("ALL",9); // Log shutdown event eventLogOSNUCAS.WriteEntry("OSNUCAS Reader is shutting down",EventLogEntryType.Error); //writeDashboardLogLine on SQL writeDashboardLogLine("OMAH799", "###########", 0, "OSNUCAS Reader is shutting down", 0, 0, 0, 0, "Stopped", "##########NUCASReader on " + Environment.MachineName + " has shut down"); // Notify support AutoEMailSender("########## NUCAS Reader is shutting down", "########### NUCAS Reader service is shutting down\n", 1); } What do I have to do to ensure that a shutdown event is logged? (Obviously, I cannot log a power outage, but a UPS generated forced shutdown due to low battery would also NOT be recorded) Thanks, Don

    C 1 Reply Last reply
    0
    • A abupsman

      I would like to request some suggestions on a problem I am having with a Windows Service I built. It installs and runs fine, using a timer every minute to check for files, then running a process. I have the process logging to a Windows Event Log, also, every time if does find and process a file it writes a record to a remote SQL server with file stats. The third thing the service does is send out e-mails. One type is a daily summary with totals and stats The second is whenever an error is caught, it collects the error data and sends it to a support group (Also writes to the event log) I also e-mail, write SQL & log at service Start and Stop. The issue is that the logging works OK at Stop if I do it from the Services panel in Admin Tools. However, if the Box is re-started by doing a Start/Shutdown/Re-start, I get no logging or e-mails to notify that the box is going down. I do get the Start up messages and logging minutes later when the system restarts. However, if someone were to do a Start/Shutdown/Shutdown, I would not be notified and it might be several hours before other processes noticed that the data stream had stopped and complain about it. Here is the code from my OnStop section: protected override void OnStop() { //Close all open connections closeResources("ALL",9); // Log shutdown event eventLogOSNUCAS.WriteEntry("OSNUCAS Reader is shutting down",EventLogEntryType.Error); //writeDashboardLogLine on SQL writeDashboardLogLine("OMAH799", "###########", 0, "OSNUCAS Reader is shutting down", 0, 0, 0, 0, "Stopped", "##########NUCASReader on " + Environment.MachineName + " has shut down"); // Notify support AutoEMailSender("########## NUCAS Reader is shutting down", "########### NUCAS Reader service is shutting down\n", 1); } What do I have to do to ensure that a shutdown event is logged? (Obviously, I cannot log a power outage, but a UPS generated forced shutdown due to low battery would also NOT be recorded) Thanks, Don

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Maybe you can try loging in the finalizer of your service (~MyServicename()) - this assumes of course that the finalizers of objects are called due to system shutdown. As you saw the Stop is not called the process is just closed.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups