Skip to content

Commit

Permalink
update example so it can run as a service
Browse files Browse the repository at this point in the history
- setting up the console writer too soon caused us to use a
ConsoleWriter instead of a LoggingWriter in service mode
  • Loading branch information
drewburlingame committed Apr 10, 2015
1 parent 51389e7 commit 90c5424
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions MultiCommandConsole.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Threading;
using Common.Logging;
using MultiCommandConsole.Services;
using MultiCommandConsole.Util;
Expand All @@ -9,9 +10,10 @@ namespace MultiCommandConsole.Example
{
class Program
{
private static readonly IConsoleWriter Writer = ConsoleWriter.Get<Program>();
private static IConsoleWriter _writer;
private static ILog _logger;

static void Main(string[] args)
static void Main(string[] args)
{
if (Array.Exists(args, s => s == "/debug") && Environment.UserInteractive)
{
Expand All @@ -21,14 +23,17 @@ static void Main(string[] args)
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

LogManager.Adapter = Log4NetFactoryAdapter.Load();
var logger = LogManager.GetLogger(typeof (Program));
logger.InfoFormat("Logging configured");
_logger = LogManager.GetLogger(typeof (Program));
_logger.InfoFormat("Logging configured");

Services.Config.EnableServiceMode();
_writer = ConsoleWriter.Get<Program>();

Config.ConsoleMode.Enabled = true;
Config.ConsoleMode.CommandPromptText = "console";
Config.ConsoleMode.HistorySize = 5;
Config.ConsoleMode.AppName = "example_console";
Config.ConsoleMode.OnBeginRunCommand += cmd => Writer.WriteLine(cmd.Dump());
Config.ConsoleMode.OnBeginRunCommand += cmd => _writer.WriteLine(cmd.Dump());

Config.ShowViewArgsCommand = true;
Config.WriteRunTimeToConsole = true;
Expand All @@ -48,7 +53,6 @@ static void Main(string[] args)
Config.Help.GetAddlHelpDelegate = (s, type) => new[] { type.FullName };

Services.Config.Defaults.CommandLine = "ping /s=google.com /i=10";
Services.Config.EnableServiceMode();

try
{
Expand All @@ -60,13 +64,20 @@ static void Main(string[] args)
}
catch (Exception e)
{
Writer.WriteLine(e.Dump());
_writer.WriteLine(e.Dump());
}
}

private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
Console.Out.WriteLine(unhandledExceptionEventArgs.ExceptionObject.Dump());
try
{
_writer.WriteErrorLine(unhandledExceptionEventArgs.ExceptionObject.Dump());
}
catch (Exception e)
{
_logger.Fatal(unhandledExceptionEventArgs.ExceptionObject.Dump());
}
((Stoplight)Config.ResolveTypeDelegate(typeof(Stoplight))).Stop();
}
}
Expand Down

0 comments on commit 90c5424

Please sign in to comment.