-3

I have 2 questions:

  1. I am creating a windows service that will export a database on one server and imports onto another server every day once a day at 3am GMT. I have installed my service and followed this: https://msdn.microsoft.com/en-us/library/sd8zc8ha(v=vs.110).aspx. However I have tried while in the code for the service and it wont allow me to attach to the process and have also tried doing it in a blank visual studio page (BOTH AS THE ADMINISTRATOR) and cannot and the problem is is when i run my service it stops for some reason so if possible I need some ideas on how i can test and debug to ensure the service Is doing what i wanted.

  2. I am using my event viewer to tell me some of the errors but this seems quite costly with time however i have received one error which is:

    Service cannot be started. System.ArgumentException: Source property was not set before writing to the event log. at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message) at DBBackUpService.BackUpService.OnStart(String[] args) in C:\Dev\Projects\DBBackUpService\DBBackUpService\BackUpService.cs:line 27 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

The code where the error starts is at the beginning of the OnStart method:

System.Timers.Timer _timer = null;
    bool backUpRunning = false;
    public BackUpService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        
        BackUpLog.WriteEntry("Database Backup has begun.");
        _timer = new System.Timers.Timer { Interval = 3600000 };
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
        _timer.Start();
        Backup();
        
    }

    protected override void OnStop()
    {
        _timer.Stop();
        BackUpLog.WriteEntry("Database Backup has finished.");
    }

    public void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        TimeSpan start = new TimeSpan(3, 0, 0);
        TimeSpan end = new TimeSpan(5, 0, 0);
        TimeSpan now = DateTime.Now.TimeOfDay;

        BackUpLog.WriteEntry("Checking if it's time to run the archive service and checking if the service is already running");


        if (backUpRunning)
            BackUpLog.WriteEntry("Service is already running");

        if ((now > start) && (now < end) && !backUpRunning)
        {
            BackUpLog.WriteEntry("Service is not already running and the time of day is valid to run the service.");
            try
            {
                backUpRunning = true;
                BackUpLog.WriteEntry("Initialising archive service");
                
            }
            catch (Exception ex)
            {
                logArchiveServiceError("The archive service failed. It will return to step 1 and try again: " + ex.Message);
            }
            finally
            {
                backUpRunning = false;
            }
        }

    }
    private void logArchiveServiceError(string errorMessage)
    {
        BackUpLog.WriteEntry(errorMessage, EventLogEntryType.Error);
        //Emailer.SendErrorAlertEmail("Archive Service Error", errorMessage, null, null);
    }

    private void logArchiveServiceInfo(string info)
    {
        BackUpLog.WriteEntry(info, EventLogEntryType.Information);
    }

TO ADD : This service has been created to export data from one database into another.

Community
  • 1
  • 1
  • Why don't you fix the exception on line 27 so you can view the events? – Dan Wilson Nov 22 '16 at 15:15
  • Well that's easier said then done in my case as i am new to creating windows services Im afraid so Im not sure how to fix it. – Sheena Rawlinson Nov 22 '16 at 15:18
  • 1
    It will be easier to help if you provide your code. – Dan Wilson Nov 22 '16 at 15:20
  • Possible duplicate of [Error while writing to event log, prevents windows service from starting?](http://stackoverflow.com/questions/4253359/error-while-writing-to-event-log-prevents-windows-service-from-starting) – Nkosi Nov 22 '16 at 15:21
  • 1
    Provide a [mcve] that reproduces the problem so that others can assist in identifying the problem and suggest possible solutions if any. – Nkosi Nov 22 '16 at 15:23
  • as requested ^^ – Sheena Rawlinson Nov 22 '16 at 15:37
  • where is `BackupLog` initialized and configured? Because if not properly configured and you try to write to event log then that would explain the error you are getting. That is the only thing in the OnStart method other than `Backup()` that would throw that exception – Nkosi Nov 22 '16 at 15:59
  • @Nkosi it is initialised in the BackUpServiceDesigner : – Sheena Rawlinson Nov 22 '16 at 16:16
  • private void InitializeComponent() { this.BackUpLog = new System.Diagnostics.EventLog(); ((System.ComponentModel.ISupportInitialize)(this.BackUpLog)).BeginInit(); //components = new System.ComponentModel.Container(); this.ServiceName = "Service1"; ((System.ComponentModel.ISupportInitialize)(this.BackUpLog)).EndInit(); } #endregion private System.Diagnostics.EventLog BackUpLog; – Sheena Rawlinson Nov 22 '16 at 16:16
  • @SheenaRawlinson What are the Source and Log properties set to? – Nkosi Nov 22 '16 at 16:19

1 Answers1

0

It doesn't appear, based on the example code provided, that you initialize the BackUpLog's source.

//...other code removed for brevity
private const string logName = "Application";

protected override void OnStart(string[] args) {
    if (!EventLog.SourceExists(this.ServiceName)) {
        EventLog.CreateEventSource(this.ServiceName, logName);
    }
    //set the The source name by which the application is registered on the local computer.
    BackUpLog.Source = this.ServiceName;
    //The name of the log the source's entries are written to. 
    //Possible values include Application, System, or a custom event log. 
    BackUpLog.Log = logName;

    BackUpLog.WriteEntry("Database Backup has begun.");
    _timer = new System.Timers.Timer { Interval = 3600000 };
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
    _timer.Start();
    Backup();

}
//...other code removed for brevity
Nkosi
  • 235,767
  • 35
  • 427
  • 472