I have 2 questions:
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.
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.