Skip to content

Commit

Permalink
Added logging to regular factory New method in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Sep 30, 2013
1 parent cfc9df1 commit 93e9c69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 30 additions & 19 deletions src/Topshelf/HostFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
// Copyright 2007-2013 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
Expand All @@ -17,6 +17,7 @@ namespace Topshelf
using HostConfigurators;
using Logging;


/// <summary>
/// Configure and run a service host using the HostFactory
/// </summary>
Expand All @@ -29,30 +30,40 @@ public static class HostFactory
/// <returns> A Topshelf service host, ready to run </returns>
public static Host New(Action<HostConfigurator> configureCallback)
{
if (configureCallback == null)
throw new ArgumentNullException("configureCallback");
try
{
if (configureCallback == null)
throw new ArgumentNullException("configureCallback");

var configurator = new HostConfiguratorImpl();
var configurator = new HostConfiguratorImpl();

Type declaringType = configureCallback.Method.DeclaringType;
if (declaringType != null)
{
string defaultServiceName = declaringType.Namespace;
if (!string.IsNullOrEmpty(defaultServiceName))
configurator.SetServiceName(defaultServiceName);
}
Type declaringType = configureCallback.Method.DeclaringType;
if (declaringType != null)
{
string defaultServiceName = declaringType.Namespace;
if (!string.IsNullOrEmpty(defaultServiceName))
configurator.SetServiceName(defaultServiceName);
}

configureCallback(configurator);
configureCallback(configurator);

configurator.ApplyCommandLine();
configurator.ApplyCommandLine();

ConfigurationResult result = ValidateConfigurationResult.CompileResults(configurator.Validate());
ConfigurationResult result = ValidateConfigurationResult.CompileResults(configurator.Validate());

if (result.Message.Length > 0)
HostLogger.Get(typeof(HostFactory))
.InfoFormat("Configuration Result:\n{0}", result.Message);
if (result.Message.Length > 0)
{
HostLogger.Get(typeof(HostFactory))
.InfoFormat("Configuration Result:\n{0}", result.Message);
}

return configurator.CreateHost();
return configurator.CreateHost();
}
catch (Exception ex)
{
HostLogger.Get(typeof(HostFactory)).Error("An exception occurred creating the host", ex);
throw;
}
}

/// <summary>
Expand All @@ -70,7 +81,7 @@ public static TopshelfExitCode Run(Action<HostConfigurator> configureCallback)
catch (Exception ex)
{
HostLogger.Get(typeof(HostFactory))
.Error("The service terminated abnormally", ex);
.Error("The service terminated abnormally", ex);

return TopshelfExitCode.AbnormalExit;
}
Expand Down

0 comments on commit 93e9c69

Please sign in to comment.