[tarantino] r158 committed - Tweak logging on Batch Jobs to optionally send the Log4Net logger to t...

3 views
Skip to first unread message

codesite...@google.com

unread,
Mar 1, 2010, 5:50:00 PM3/1/10
to tarantino...@googlegroups.com
Revision: 158
Author: ajepst
Date: Mon Mar 1 14:48:26 2010
Log: Tweak logging on Batch Jobs to optionally send the Log4Net logger to
the spawned JobAgent (if it requires it). Also, adding a bit more logging
http://code.google.com/p/tarantino/source/detail?r=158

Added:
/trunk/src/BatchJobs.Core/ILogger.cs
Modified:
/trunk/src/BatchJobs.Console/Logger.cs
/trunk/src/BatchJobs.Console/Program.cs
/trunk/src/BatchJobs.Core/BatchJobs.Core.csproj
/trunk/src/BatchJobs.Core/JobAgentBase.cs
/trunk/src/BatchJobs.UnitTests/ProgramTester.cs

=======================================
--- /dev/null
+++ /trunk/src/BatchJobs.Core/ILogger.cs Mon Mar 1 14:48:26 2010
@@ -0,0 +1,14 @@
+using System;
+
+namespace BatchJobs.Core
+{
+ public interface ILogger
+ {
+ string CONFIG_FILE_NAME { get; }
+ void Error(object source, object message);
+ void Warn(object source, object message);
+ void Info(object source, object message);
+ void Fatal(object source, object message);
+ void Debug(object source, object message);
+ }
+}
=======================================
--- /trunk/src/BatchJobs.Console/Logger.cs Fri Feb 26 14:56:10 2010
+++ /trunk/src/BatchJobs.Console/Logger.cs Mon Mar 1 14:48:26 2010
@@ -1,12 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
+using BatchJobs.Core;
using log4net;
using log4net.Config;

namespace BatchJobs.Console
{
- public static class Logger
+ public class LoggerProxy : ILogger
+ {
+ public string CONFIG_FILE_NAME
+ {
+ get { return Logger.CONFIG_FILE_NAME; }
+ }
+
+ public void Error(object source, object message)
+ {
+ Logger.Error(source, message);
+ }
+
+ public void Warn(object source, object message)
+ {
+ Logger.Warn(source, message);
+ }
+
+ public void Info(object source, object message)
+ {
+ Logger.Info(source, message);
+ }
+
+ public void Fatal(object source, object message)
+ {
+ Logger.Fatal(source, message);
+ }
+
+ public void Debug(object source, object message)
+ {
+ Logger.Debug(source, message);
+ }
+ }
+
+
+ public static class Logger
{
private static bool _logInitialized;
public const string CONFIG_FILE_NAME = "Log4Net.config";
=======================================
--- /trunk/src/BatchJobs.Console/Program.cs Fri Feb 26 14:56:10 2010
+++ /trunk/src/BatchJobs.Console/Program.cs Mon Mar 1 14:48:26 2010
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.IO;
+using System.Linq;
using System.Net.Mail;
using System.Reflection;
using BatchJobs.Core;
@@ -47,7 +48,16 @@
Logger.Debug(this, string.Format("Command Line Specified Instance
Name: {0}", args[0]));
IJobAgent jobAgent = Factory().Create(args[0]);
Logger.Debug(this, "Executing the Job");
- jobAgent.Execute();
+ try
+ {
+ jobAgent.Execute();
+ Logger.Debug(this, string.Format("Job Execution Complete: {0}",
args[0]));
+ }
+ catch ( Exception e)
+ {
+ Logger.Fatal(this, Logger.SerializeException(e));
+ throw;
+ }
}
}

@@ -82,7 +92,12 @@
}
Type classType = a.GetType(typename);
Logger.Debug(this, string.Format("Creating instance of {0}",
classType));
+ if (classType.GetConstructors().Any( x => x.GetParameters().Length ==
1))
+ {
+ return (IJobAgentFactory)Activator.CreateInstance(classType, new
LoggerProxy());
+ }
return (IJobAgentFactory)Activator.CreateInstance(classType);
+
}
}

=======================================
--- /trunk/src/BatchJobs.Core/BatchJobs.Core.csproj Fri Feb 26 14:56:10 2010
+++ /trunk/src/BatchJobs.Core/BatchJobs.Core.csproj Mon Mar 1 14:48:26 2010
@@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IJobAgentFactory.cs" />
+ <Compile Include="ILogger.cs" />
<Compile Include="JobAgentBase.cs" />
<Compile Include="IJobAgent.cs" />
<Compile Include="IStateTransitionFactory.cs" />
=======================================
--- /trunk/src/BatchJobs.Core/JobAgentBase.cs Fri Feb 26 14:56:10 2010
+++ /trunk/src/BatchJobs.Core/JobAgentBase.cs Mon Mar 1 14:48:26 2010
@@ -29,14 +29,7 @@
if (transition.IsValid(batch))
{
Logger.Debug(this, string.Format("Transition {0} is valid for batch
{1}, executing", transition, batch));
- try
- {
- transition.Execute(batch);
- } catch(Exception e)
- {
- Logger.Fatal(this, Logger.SerializeException(e));
- throw;
- }
+ transition.Execute(batch);
}
}
}
=======================================
--- /trunk/src/BatchJobs.UnitTests/ProgramTester.cs Tue Dec 15 07:56:03 2009
+++ /trunk/src/BatchJobs.UnitTests/ProgramTester.cs Mon Mar 1 14:48:26 2010
@@ -30,6 +30,16 @@
Assert.That(FactoryStub.Name, Is.EqualTo("foo"));
Assert.That(agent.Executed, Is.True);
}
+
+ [Test]
+ public void Should_create_a_logging_factory()
+ {
+ var program = new Program();
+ program.GetFactoryTypeName = () => typeof(FactoryLoggingStub).FullName
+ "," + GetType().Assembly.FullName;
+ IJobAgentFactory factory = program.Factory();
+ Assert.AreEqual("BatchJobs.UnitTests.FactoryLoggingStub",
factory.GetType().ToString());
+
+ }
}

public class StubJob : IJobAgent
@@ -42,6 +52,19 @@
Executed = true;
}
}
+
+ public class StubJobWithContructorParam : IJobAgent
+ {
+
+ public StubJobWithContructorParam(ILogger logger)
+ {
+ }
+
+ public void Execute()
+ {
+
+ }
+ }

public class FactoryStub : IJobAgentFactory
{
@@ -70,4 +93,39 @@
return new string[0];
}
}
-}
+
+ public class FactoryLoggingStub : IJobAgentFactory
+ {
+ private readonly ILogger _logger;
+
+ public FactoryLoggingStub(ILogger logger)
+ {
+ _logger = logger;
+ }
+
+ public FactoryLoggingStub()
+ {
+ if (JobAgent == null)
+ {
+ JobAgent = new StubJobWithContructorParam(_logger);
+ }
+ }
+
+ public static IJobAgent JobAgent { get; set; }
+
+ public static string Name { get; set; }
+
+
+ public IJobAgent Create(string name)
+ {
+ System.Console.WriteLine(name);
+ Name = name;
+ return JobAgent;
+ }
+
+ public IEnumerable<string> GetInstanceNames()
+ {
+ return new string[0];
+ }
+ }
+}

Reply all
Reply to author
Forward
0 new messages