|
#region Copyright
// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
//
// This file is part of Diagnostic Explorer.
//
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Diagnostic Explorer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer. If not, see <http://www.gnu.org/licenses/>.
//
// http://diagexplorer.sourceforge.net/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using log4net;
using log4net.Config;
using System.Diagnostics;
using DiagnosticExplorer.WinService;
namespace DiagnosticExplorer.WindowsService
{
[DiagnosticClass(AttributedPropertiesOnly = true)]
public class DiagnosticWinService : ServiceBase
{
private ILog _log = LogManager.GetLogger(typeof (DiagnosticWinService));
private ServiceHost _svcHost;
private bool _isStarted;
public DiagnosticWinService()
{
DiagnosticManager.Register(this, "Diagnostic WinService", "System");
}
protected override void OnStart(string[] args)
{
_log.Info("Starting Service");
try
{
_isStarted = true;
string configDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
string configPath = Path.Combine(configDir, "log4net.config");
XmlConfigurator.ConfigureAndWatch(new FileInfo(configPath));
DiagnosticHostingService.Start();
OpenServiceHost(0);
}
catch (Exception ex)
{
File.WriteAllText(@"d:\Error.txt", ex.ToString());
Trace.WriteLine(ex);
_log.Error("An exception was thrown during service startup", ex);
throw;
}
}
private void OpenServiceHost(int sleepMillis)
{
if (sleepMillis > 0)
Thread.Sleep(sleepMillis);
if (_isStarted)
{
_svcHost = new ServiceHost(typeof(DiagnosticLogger));
_svcHost.Faulted += HandleHostFaulted;
_svcHost.BeginOpen(OpenFinished, null);
}
}
public string ServiceHostStatus
{
get { return _svcHost == null ? "NULL" : _svcHost.State.ToString(); }
}
private void OpenFinished(IAsyncResult result)
{
}
private void HandleHostFaulted(object sender, EventArgs e)
{
_log.Info("Host faulted");
Action action = () => OpenServiceHost(2000);
action.BeginInvoke(null, null);
}
protected override void OnStop()
{
_log.Info("DBDiagService Stopping");
try
{
_isStarted = false;
DiagnosticHostingService.Stop();
if (_svcHost != null && _svcHost.State == CommunicationState.Opened)
{
_svcHost.Close();
_svcHost = null;
}
}
catch (Exception ex)
{
_log.Error("An exception was thrown during service shutdown", ex);
// don't rethrow - an unhandled exception during service shutdown might leave it running
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am a software developer originally from Auckland, New Zealand. I have lived and worked in London since 2005.