Browse files

Create interfaces for ILogManager and ILogger & make intance based

  • Loading branch information...
1 parent fe7afa6 commit eaf1f27b534fbbbedf6b674f9bb2099dbd2cb95e @onovotny committed Sep 7, 2012
View
2 MetroLog.NetCore/LogConfig.cs
@@ -15,7 +15,7 @@ public static void InitializeDefault()
configuration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new DebugTarget());
configuration.AddTarget(LogLevel.Error, LogLevel.Fatal, new FileSnapshotTarget());
- LogManager.Reset(configuration);
+ // LogManager.Reset(configuration);
}
}
}
View
9 MetroLog.Tests/MetroLog.Tests.csproj
@@ -124,6 +124,9 @@
<AppxManifest Include="Package.appxmanifest">
<SubType>Designer</SubType>
</AppxManifest>
+ <None Include="..\MetroLog.snk">
+ <Link>MetroLog.snk</Link>
+ </None>
<None Include="MetroLog.Tests_TemporaryKey.pfx" />
</ItemGroup>
<ItemGroup>
@@ -153,6 +156,12 @@
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '11.0' ">
<VisualStudioVersion>11.0</VisualStudioVersion>
</PropertyGroup>
+ <PropertyGroup>
+ <SignAssembly>true</SignAssembly>
+ </PropertyGroup>
+ <PropertyGroup>
+ <AssemblyOriginatorKeyFile>..\MetroLog.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
18 MetroLog.Tests/Objects/TestLoggable.cs
@@ -6,14 +6,14 @@
namespace MetroLog.Tests
{
- public class TestLoggable : ILoggable
- {
- public void DoMagic()
- {
- this.Info("In this case, Info is an extension method...");
+ //public class TestLoggable : ILoggable
+ //{
+ // public void DoMagic()
+ // {
+ // this.Info("In this case, Info is an extension method...");
- var buf = "like this";
- this.Warn("You can also use formatting, {0}", buf);
- }
- }
+ // var buf = "like this";
+ // this.Warn("You can also use formatting, {0}", buf);
+ // }
+ //}
}
View
17 MetroLog.Tests/Objects/TestTarget.cs
@@ -12,23 +12,12 @@ internal class TestTarget : SyncTarget
{
internal int NumWritten { get; private set; }
internal LogEventInfo LastWritten { get; private set; }
-
- private static TestTarget _current = new TestTarget();
-
- private TestTarget()
+
+ public TestTarget()
: base(new SingleLineLayout())
{
}
-
- internal static TestTarget Current
- {
- get
- {
- if(_current == null)
- throw new ObjectDisposedException("TestTarget");
- return _current;
- }
- }
+
protected override void Write(LogEventInfo entry)
{
View
29 MetroLog.Tests/Tests/BrokenTargetTests.cs
@@ -10,29 +10,26 @@ namespace MetroLog.Tests
[TestClass]
public class BrokenTargetTests
{
- [TestInitialize]
- public void Initialize()
- {
- LogManager.Reset();
-
- // add a broken target, then the normal target...
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new BrokenTarget());
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, TestTarget.Current);
- }
[TestMethod]
- public void TestBrokenTarget()
+ public async Task TestBrokenTarget()
{
- TestTarget.Current.Reset();
+ var testTarget = new TestTarget();
+
+ var config = new LoggingConfiguration();
+ config.AddTarget(LogLevel.Trace, LogLevel.Fatal, new BrokenTarget());
+ config.AddTarget(LogLevel.Trace, LogLevel.Fatal, testTarget);
+
+ var target = new LogManager(config);
// this should ignore errors in the broken target and flip down to the working target...
- var logger = LogManager.GetLogger("Foobar");
- logger.Trace("Hello, world.");
+ var logger = (Logger)target.GetLogger("Foobar");
+ await logger.TraceAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, testTarget.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, testTarget.LastWritten.Level);
+ Assert.IsNull(testTarget.LastWritten.Exception);
}
}
}
View
11 MetroLog.Tests/Tests/FileSnapshotTests.cs
@@ -17,18 +17,17 @@ public class FileSnapshotTests
[TestMethod]
public async Task TestFileSnapshot()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Fatal, new FileSnapshotTarget());
+ var target = new FileSnapshotTarget();
// send through a log entry...
- var loggable = new TestLoggable();
- var op = await loggable.Fatal("Testing file write...", new InvalidOperationException("An exception message..."));
+ var op = await target.WriteAsync(new LogEventInfo(LogLevel.Fatal, "TestLogger", "Testing file write...", new InvalidOperationException("An exception message...")));
+
+ // TODO: This should be Faked! We shouldn't be writing to the disk
// load the file...
var folder = await FileSnapshotTarget.EnsureInitializedAsync();
var files = await folder.GetFilesAsync();
- var file = files.Where(v => v.Name.Contains(op[0].Entry.SequenceID.ToString())).First();
+ var file = files.First(v => v.Name.Contains(op.Entry.SequenceID.ToString()));
string contents = await FileIO.ReadTextAsync(file);
// check...
View
565 MetroLog.Tests/Tests/ILoggableTests.cs
@@ -7,290 +7,283 @@
namespace MetroLog.Tests
{
- [TestClass]
- public class ILoggableTests
- {
- [TestInitialize]
- public void Initialize()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, TestTarget.Current);
- }
-
- [TestMethod]
- public void TestTraceForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Trace("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestDebugForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Debug("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestInfoForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Info("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestWarnForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Warn("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestErrorForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Error("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestFatalForLoggable()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Fatal("Hello, world.");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestTraceForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Trace("Hello, world.", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestDebugForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Debug("Hello, world", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestInfoForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Info("Hello, world", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestWarnForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Warn("Hello, world", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestErrorForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Error("Hello, world", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestFatalForLoggableWithException()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Fatal("Hello, world", new InvalidOperationException("Foobar"));
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
- }
-
- [TestMethod]
- public void TestTraceForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Trace("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
-
- [TestMethod]
- public void TestDebugForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Debug("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
-
- [TestMethod]
- public void TestInfoForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Info("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
-
- [TestMethod]
- public void TestWarnForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Warn("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
-
- [TestMethod]
- public void TestErrorForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Error("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
-
- [TestMethod]
- public void TestFatalForLoggableWithFormat()
- {
- TestTarget.Current.Reset();
-
- // run...
- var loggable = new TestLoggable();
- loggable.Fatal("Hello, {0}.", "**foo**");
-
- // check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
- }
- }
+ //[TestClass]
+ //public class ILoggableTests
+ //{
+ // [TestMethod]
+ // public void TestTraceForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Trace("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestDebugForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Debug("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestInfoForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Info("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestWarnForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Warn("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestErrorForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Error("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestFatalForLoggable()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Fatal("Hello, world.");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestTraceForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Trace("Hello, world.", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestDebugForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Debug("Hello, world", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestInfoForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Info("Hello, world", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestWarnForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Warn("Hello, world", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestErrorForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Error("Hello, world", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestFatalForLoggableWithException()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Fatal("Hello, world", new InvalidOperationException("Foobar"));
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ // }
+
+ // [TestMethod]
+ // public void TestTraceForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Trace("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+
+ // [TestMethod]
+ // public void TestDebugForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Debug("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+
+ // [TestMethod]
+ // public void TestInfoForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Info("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+
+ // [TestMethod]
+ // public void TestWarnForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Warn("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+
+ // [TestMethod]
+ // public void TestErrorForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Error("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+
+ // [TestMethod]
+ // public void TestFatalForLoggableWithFormat()
+ // {
+ // TestTarget.Current.Reset();
+
+ // // run...
+ // var loggable = new TestLoggable();
+ // loggable.Fatal("Hello, {0}.", "**foo**");
+
+ // // check...
+ // Assert.AreEqual(1, TestTarget.Current.NumWritten);
+ // Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
+ // Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ // Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ // }
+ //}
}
View
339 MetroLog.Tests/Tests/LevelTests.cs
@@ -10,15 +10,24 @@ namespace MetroLog.Tests
[TestClass]
public class LevelTests
{
+ private Tuple<ILogManager, TestTarget> CreateWithLevel(LogLevel min, LogLevel max)
+ {
+ var testTarget = new TestTarget();
+
+ var config = new LoggingConfiguration();
+ config.AddTarget(min, max, testTarget);
+
+ return Tuple.Create<ILogManager, TestTarget>(new LogManager(config), testTarget);
+ }
+
[TestMethod]
public void TestIsTraceEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Debug, LogLevel.Fatal, TestTarget.Current);
+
+ var target = CreateWithLevel(LogLevel.Debug, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.Item1.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -30,32 +39,27 @@ public void TestIsTraceEnabled()
}
[TestMethod]
- public void TestTraceIgnored()
+ public async Task TestTraceIgnored()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Debug, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Debug, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = (Logger)target.Item1.GetLogger("foo");
// run...
- TestTarget.Current.Reset();
- logger.Trace("Foobar");
+ await logger.TraceAsync("Foobar");
// check...
- Assert.AreEqual(0, TestTarget.Current.NumWritten);
+ Assert.AreEqual(0, target.Item2.NumWritten);
}
[TestMethod]
public void TestIsDebugEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Info, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.Item1.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -67,32 +71,27 @@ public void TestIsDebugEnabled()
}
[TestMethod]
- public void TestDebugIgnored()
+ public async Task TestDebugIgnored()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Info, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = (Logger)target.Item1.GetLogger("foo");
// run...
- TestTarget.Current.Reset();
- logger.Debug("Foobar");
+ await logger.DebugAsync("Foobar");
// check...
- Assert.AreEqual(0, TestTarget.Current.NumWritten);
+ Assert.AreEqual(0, target.Item2.NumWritten);
}
[TestMethod]
public void TestIsInfoEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Warn, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Warn, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.Item1.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -104,32 +103,27 @@ public void TestIsInfoEnabled()
}
[TestMethod]
- public void TestInfoIgnored()
+ public async Task TestInfoIgnored()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Warn, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Warn, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = (Logger)target.Item1.GetLogger("foo");
// run...
- TestTarget.Current.Reset();
- logger.Info("Foobar");
+ await logger.InfoAsync("Foobar");
// check...
- Assert.AreEqual(0, TestTarget.Current.NumWritten);
+ Assert.AreEqual(0, target.Item2.NumWritten);
}
[TestMethod]
public void TestIsWarnEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Error, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Error, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.Item1.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -141,32 +135,27 @@ public void TestIsWarnEnabled()
}
[TestMethod]
- public void TestWarnIgnored()
+ public async Task TestWarnIgnored()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Error, LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Error, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = (Logger)target.Item1.GetLogger("foo");
// run...
- TestTarget.Current.Reset();
- logger.Warn("Foobar");
-
+ await logger.WarnAsync("Foobar");
+
// check...
- Assert.AreEqual(0, TestTarget.Current.NumWritten);
+ Assert.AreEqual(0, target.Item2.NumWritten);
}
[TestMethod]
public void TestIsErrorEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Fatal, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.Item1.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -178,31 +167,27 @@ public void TestIsErrorEnabled()
}
[TestMethod]
- public void TestErrorIgnored()
+ public async Task TestErrorIgnored()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Fatal, TestTarget.Current);
+ var target = CreateWithLevel(LogLevel.Fatal, LogLevel.Fatal);
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = (Logger)target.Item1.GetLogger("foo");
// run...
- TestTarget.Current.Reset();
- logger.Error("Foobar");
+ await logger.ErrorAsync("Foobar");
// check...
- Assert.AreEqual(0, TestTarget.Current.NumWritten);
+ Assert.AreEqual(0, target.Item2.NumWritten);
}
[TestMethod]
public void TestIsFatalEnabled()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
+ var target = new LogManager(new LoggingConfiguration());
// get a logger...
- var logger = LogManager.GetLogger("foo");
+ var logger = target.GetLogger("foo");
// check...
Assert.IsFalse(logger.IsTraceEnabled);
@@ -213,117 +198,119 @@ public void TestIsFatalEnabled()
Assert.IsFalse(logger.IsFatalEnabled);
}
- [TestMethod]
- public void TestIsTraceEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Debug, LogLevel.Fatal, TestTarget.Current);
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsTrue(loggable.IsDebugEnabled());
- Assert.IsTrue(loggable.IsInfoEnabled());
- Assert.IsTrue(loggable.IsWarnEnabled());
- Assert.IsTrue(loggable.IsErrorEnabled());
- Assert.IsTrue(loggable.IsFatalEnabled());
- }
-
- [TestMethod]
- public void TestIsDebugEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, TestTarget.Current);
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsFalse(loggable.IsDebugEnabled());
- Assert.IsTrue(loggable.IsInfoEnabled());
- Assert.IsTrue(loggable.IsWarnEnabled());
- Assert.IsTrue(loggable.IsErrorEnabled());
- Assert.IsTrue(loggable.IsFatalEnabled());
- }
-
- [TestMethod]
- public void TestIsInfoEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Warn, LogLevel.Fatal, TestTarget.Current);
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsFalse(loggable.IsDebugEnabled());
- Assert.IsFalse(loggable.IsInfoEnabled());
- Assert.IsTrue(loggable.IsWarnEnabled());
- Assert.IsTrue(loggable.IsErrorEnabled());
- Assert.IsTrue(loggable.IsFatalEnabled());
- }
-
- [TestMethod]
- public void TestIsWarnEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Error, LogLevel.Fatal, TestTarget.Current);
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsFalse(loggable.IsDebugEnabled());
- Assert.IsFalse(loggable.IsInfoEnabled());
- Assert.IsFalse(loggable.IsWarnEnabled());
- Assert.IsTrue(loggable.IsErrorEnabled());
- Assert.IsTrue(loggable.IsFatalEnabled());
- }
-
- [TestMethod]
- public void TestIsErrorEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Fatal, TestTarget.Current);
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsFalse(loggable.IsDebugEnabled());
- Assert.IsFalse(loggable.IsInfoEnabled());
- Assert.IsFalse(loggable.IsWarnEnabled());
- Assert.IsFalse(loggable.IsErrorEnabled());
- Assert.IsTrue(loggable.IsFatalEnabled());
- }
-
- [TestMethod]
- public void TestIsFatalEnabledForLoggable()
- {
- LogManager.Reset();
- LogManager.DefaultConfiguration.ClearTargets();
-
- // get a loggable...
- var loggable = new TestLoggable();
-
- // check...
- Assert.IsFalse(loggable.IsTraceEnabled());
- Assert.IsFalse(loggable.IsDebugEnabled());
- Assert.IsFalse(loggable.IsInfoEnabled());
- Assert.IsFalse(loggable.IsWarnEnabled());
- Assert.IsFalse(loggable.IsErrorEnabled());
- Assert.IsFalse(loggable.IsFatalEnabled());
- }
+ //[TestMethod]
+ //public void TestIsTraceEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+ // LogManager.DefaultConfiguration.AddTarget(LogLevel.Debug, LogLevel.Fatal, TestTarget.Current);
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsTrue(loggable.IsDebugEnabled());
+ // Assert.IsTrue(loggable.IsInfoEnabled());
+ // Assert.IsTrue(loggable.IsWarnEnabled());
+ // Assert.IsTrue(loggable.IsErrorEnabled());
+ // Assert.IsTrue(loggable.IsFatalEnabled());
+ //}
+
+ //[TestMethod]
+ //public void TestIsDebugEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+ // LogManager.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, TestTarget.Current);
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsFalse(loggable.IsDebugEnabled());
+ // Assert.IsTrue(loggable.IsInfoEnabled());
+ // Assert.IsTrue(loggable.IsWarnEnabled());
+ // Assert.IsTrue(loggable.IsErrorEnabled());
+ // Assert.IsTrue(loggable.IsFatalEnabled());
+ //}
+
+ //[TestMethod]
+ //public void TestIsInfoEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+ // LogManager.DefaultConfiguration.AddTarget(LogLevel.Warn, LogLevel.Fatal, TestTarget.Current);
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsFalse(loggable.IsDebugEnabled());
+ // Assert.IsFalse(loggable.IsInfoEnabled());
+ // Assert.IsTrue(loggable.IsWarnEnabled());
+ // Assert.IsTrue(loggable.IsErrorEnabled());
+ // Assert.IsTrue(loggable.IsFatalEnabled());
+ //}
+
+ //[TestMethod]
+ //public void TestIsWarnEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+ // LogManager.DefaultConfiguration.AddTarget(LogLevel.Error, LogLevel.Fatal, TestTarget.Current);
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsFalse(loggable.IsDebugEnabled());
+ // Assert.IsFalse(loggable.IsInfoEnabled());
+ // Assert.IsFalse(loggable.IsWarnEnabled());
+ // Assert.IsTrue(loggable.IsErrorEnabled());
+ // Assert.IsTrue(loggable.IsFatalEnabled());
+ //}
+
+ //[TestMethod]
+ //public void TestIsErrorEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+ // LogManager.DefaultConfiguration.AddTarget(LogLevel.Fatal, TestTarget.Current);
+
+
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsFalse(loggable.IsDebugEnabled());
+ // Assert.IsFalse(loggable.IsInfoEnabled());
+ // Assert.IsFalse(loggable.IsWarnEnabled());
+ // Assert.IsFalse(loggable.IsErrorEnabled());
+ // Assert.IsTrue(loggable.IsFatalEnabled());
+ //}
+
+ //[TestMethod]
+ //public void TestIsFatalEnabledForLoggable()
+ //{
+ // LogManager.Reset();
+ // LogManager.DefaultConfiguration.ClearTargets();
+
+ // // get a loggable...
+ // var loggable = new TestLoggable();
+
+ // // check...
+ // Assert.IsFalse(loggable.IsTraceEnabled());
+ // Assert.IsFalse(loggable.IsDebugEnabled());
+ // Assert.IsFalse(loggable.IsInfoEnabled());
+ // Assert.IsFalse(loggable.IsWarnEnabled());
+ // Assert.IsFalse(loggable.IsErrorEnabled());
+ // Assert.IsFalse(loggable.IsFatalEnabled());
+ //}
}
}
View
4 MetroLog.Tests/Tests/LogManagerTests.cs
@@ -12,7 +12,9 @@ public class LogManagerTests
[TestMethod]
public void TestGetLogger()
{
- var logger = LogManager.GetLogger("Foobar");
+ var target = new LogManager();
+
+ var logger = target.GetLogger("Foobar");
Assert.IsNotNull(logger);
}
}
View
298 MetroLog.Tests/Tests/LoggerTests.cs
@@ -10,287 +10,281 @@ namespace MetroLog.Tests
[TestClass]
public class LoggerTests
{
- [TestInitialize]
- public void Initialize()
+ private static Tuple<Logger, TestTarget> CreateTarget()
{
- LogManager.Reset();
- LogManager.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, TestTarget.Current);
+ var testTarget = new TestTarget();
+ var config = new LoggingConfiguration();
+ config.AddTarget(LogLevel.Trace, LogLevel.Fatal, testTarget);
+
+ return Tuple.Create(new Logger("Foobar", config), testTarget);
}
[TestMethod]
- public void TestTrace()
+ public async Task TestTrace()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Trace("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.TraceAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestDebug()
+ public async Task TestLog()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Debug("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.LogAsync(LogLevel.Trace, "Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
+
[TestMethod]
- public void TestInfo()
+ public async Task TestDebug()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Info("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.DebugAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Debug, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestWarn()
+ public async Task TestInfo()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Warn("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.InfoAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Info, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestError()
+ public async Task TestWarn()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Error("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.WarnAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Warn, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestFatal()
+ public async Task TestError()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Fatal("Hello, world.");
+ var logger = CreateTarget();
+ await logger.Item1.ErrorAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Error, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestTraceWithException()
+ public async Task TestFatal()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Trace("Hello, world.", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.FatalAsync("Hello, world.");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Fatal, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestDebugWithException()
+ public async Task TestTraceWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Debug("Hello, world", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.TraceAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestInfoWithException()
+ public async Task TestLogWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Info("Hello, world", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.LogAsync(LogLevel.Trace, "Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestWarnWithException()
+ public async Task TestDebugWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Warn("Hello, world", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.DebugAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Debug, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestErrorWithException()
+ public async Task TestInfoWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Error("Hello, world", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.InfoAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Info, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestFatalWithException()
+ public async Task TestWarnWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Fatal("Hello, world", new InvalidOperationException("Foobar"));
+ var logger = CreateTarget();
+ await logger.Item1.WarnAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNotNull(TestTarget.Current.LastWritten.Exception);
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Warn, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestTraceWithFormat()
+ public async Task TestErrorWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Trace("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.ErrorAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Trace, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Error, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestDebugWithFormat()
+ public async Task TestFatalWithException()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Debug("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.FatalAsync("Hello, world.", new InvalidOperationException("Foobar"));
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Debug, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Fatal, logger.Item2.LastWritten.Level);
+ Assert.IsNotNull(logger.Item2.LastWritten.Exception);
}
[TestMethod]
- public void TestInfoWithFormat()
+ public async Task TestTraceWithFormat()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Info("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.TraceAsync("Hello, {0}.", "**foo**");
+
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Info, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Trace, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
}
[TestMethod]
- public void TestWarnWithFormat()
+ public async Task TestDebugWithFormat()
{
- TestTarget.Current.Reset();
-
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Warn("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.DebugAsync("Hello, {0}.", "**foo**");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Warn, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Debug, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
}
[TestMethod]
- public void TestErrorWithFormat()
+ public async Task TestInfoWithFormat()
{
- TestTarget.Current.Reset();
+ // run...
+ var logger = CreateTarget();
+ await logger.Item1.InfoAsync("Hello, {0}.", "**foo**");
+ // check...
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Info, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
+ }
+
+ [TestMethod]
+ public async Task TestWarnWithFormat()
+ {
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Error("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.WarnAsync("Hello, {0}.", "**foo**");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Error, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Warn, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
}
[TestMethod]
- public void TestFatalWithFormat()
+ public async Task TestErrorWithFormat()
{
- TestTarget.Current.Reset();
+ // run...
+ var logger = CreateTarget();
+ await logger.Item1.ErrorAsync("Hello, {0}.", "**foo**");
+ // check...
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Error, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
+ }
+
+ [TestMethod]
+ public async Task TestFatalWithFormat()
+ {
// run...
- var logger = LogManager.GetLogger("Foobar");
- logger.Fatal("Hello, {0}.", "**foo**");
+ var logger = CreateTarget();
+ await logger.Item1.FatalAsync("Hello, {0}.", "**foo**");
// check...
- Assert.AreEqual(1, TestTarget.Current.NumWritten);
- Assert.AreEqual(LogLevel.Fatal, TestTarget.Current.LastWritten.Level);
- Assert.IsNull(TestTarget.Current.LastWritten.Exception);
- Assert.AreNotEqual(-1, TestTarget.Current.LastWritten.Message.IndexOf("**foo**"));
+ Assert.AreEqual(1, logger.Item2.NumWritten);
+ Assert.AreEqual(LogLevel.Fatal, logger.Item2.LastWritten.Level);
+ Assert.IsNull(logger.Item2.LastWritten.Exception);
+ Assert.AreNotEqual(-1, logger.Item2.LastWritten.Message.IndexOf("**foo**"));
}
}
}
View
1 MetroLog.sln.DotSettings
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+ <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckNamespace/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpUsing/KeepImports/=System_002EThreading_002ETasks/@EntryIndexedValue">System.Threading.Tasks</s:String></wpf:ResourceDictionary>
View
4 MetroLog/MetroLog.csproj
@@ -107,9 +107,7 @@
<Compile Include="Layouts\FileSnapshotLayout.cs" />
<Compile Include="Layouts\Layout.cs" />
<Compile Include="Layouts\SingleLineLayout.cs" />
- <Compile Include="Objects\FrameworkLogger.cs" />
- <Compile Include="Objects\ILoggable.cs" />
- <Compile Include="Objects\ILoggableExtender.cs" />
+ <Compile Include="Objects\ILogger.cs" />
<Compile Include="Objects\ILogManager.cs" />
<Compile Include="Objects\LogEventInfo.cs" />
<Compile Include="Objects\Logger.cs" />
View
12 MetroLog/Objects/FrameworkLogger.cs
@@ -1,12 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace MetroLog.Objects
-{
- class FrameworkLogger
- {
- }
-}
View
6 MetroLog/Objects/ILogManager.cs
@@ -4,9 +4,13 @@
using System.Text;
using System.Threading.Tasks;
-namespace MetroLog.Objects
+namespace MetroLog
{
public interface ILogManager
{
+ LoggingConfiguration DefaultConfiguration { get; }
+ // ILogger GetLogger(ILoggable loggable, LoggingConfiguration config);
+ ILogger GetLogger<T>(LoggingConfiguration config = null);
+ ILogger GetLogger(string name, LoggingConfiguration config = null);
}
}
View
13 MetroLog/Objects/ILoggable.cs
@@ -1,13 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace MetroLog
-{
- // use this interface to bind the logging extension methods...
- public interface ILoggable
- {
- }
-}
View
124 MetroLog/Objects/ILoggableExtender.cs
@@ -1,124 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace MetroLog
-{
- public static class ILoggableExtender
- {
- public static Task<LogWriteOperation[]> Trace(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Trace, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Trace(this ILoggable loggable, string message, params string[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Trace, message, ps);
- }
-
- public static Task<LogWriteOperation[]> Debug(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Debug, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Debug(this ILoggable loggable, string message, params string[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Debug, message, ps);
- }
-
- public static Task<LogWriteOperation[]> Info(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Info, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Info(this ILoggable loggable, string message, params object[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Info, message, ps);
- }
-
- public static Task<LogWriteOperation[]> Warn(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Warn, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Warn(this ILoggable loggable, string message, params string[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Warn, message, ps);
- }
-
- public static Task<LogWriteOperation[]> Error(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Error, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Error(this ILoggable loggable, string message, params string[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Error, message, ps);
- }
-
- public static Task<LogWriteOperation[]> Fatal(this ILoggable loggable, string message, Exception ex = null)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Fatal, message, ex);
- }
-
- public static Task<LogWriteOperation[]> Fatal(this ILoggable loggable, string message, params string[] ps)
- {
- var logger = loggable.GetLogger();
- return logger.Log(LogLevel.Fatal, message, ps);
- }
-
- public static Logger GetLogger(this ILoggable loggable, LoggingConfiguration config = null)
- {
- return LogManager.GetLogger(loggable, config);
- }
-
- public static bool IsTraceEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Trace);
- }
-
- public static bool IsDebugEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Debug);
- }
-
- public static bool IsInfoEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Info);
- }
-
- public static bool IsWarnEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Warn);
- }
-
- public static bool IsErrorEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Error);
- }
-
- public static bool IsFatalEnabled(this ILoggable loggable)
- {
- return loggable.IsLevelEnabled(LogLevel.Fatal);
- }
-
- public static bool IsLevelEnabled(this ILoggable loggable, LogLevel level)
- {
- var logger = loggable.GetLogger();
- return logger.IsEnabled(level);
- }
- }
-}
View
31 MetroLog/Objects/ILogger.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Threading.Tasks;
+
+namespace MetroLog
+{
+ public interface ILogger
+ {
+ string Name { get; }
+ bool IsTraceEnabled { get; }
+ bool IsDebugEnabled { get; }
+ bool IsInfoEnabled { get; }
+ bool IsWarnEnabled { get; }
+ bool IsErrorEnabled { get; }
+ bool IsFatalEnabled { get; }
+ void Trace(string message, Exception ex = null);
+ void Trace(string message, params object[] ps);
+ void Debug(string message, Exception ex = null);
+ void Debug(string message, params object[] ps);
+ void Info(string message, Exception ex = null);
+ void Info(string message, params object[] ps);
+ void Warn(string message, Exception ex = null);
+ void Warn(string message, params object[] ps);
+ void Error(string message, Exception ex = null);
+ void Error(string message, params object[] ps);
+ void Fatal(string message, Exception ex = null);
+ void Fatal(string message, params object[] ps);
+ void Log(LogLevel logLevel, string message, Exception ex);
+ void Log(LogLevel logLevel, string message, params object[] ps);
+ bool IsEnabled(LogLevel level);
+ }
+}
View
22 MetroLog/Objects/LogEventInfo.cs
@@ -20,22 +20,12 @@ public class LogEventInfo
internal LogEventInfo(LogLevel level, string logger, string message, Exception ex)
{
- this.Level = level;
- this.Logger = logger;
- this.Message = message;
- this.Exception = ex;
- this.TimeStamp = LogManager.GetDateTime();
- this.SequenceID = Interlocked.Increment(ref _globalSequenceId);
- }
-
- internal LogEventInfo Clone()
- {
- var newInfo = new LogEventInfo(this.Level, this.Logger, this.Message, this.Exception);
- newInfo.TimeStamp = this.TimeStamp;
- newInfo.SequenceID = this.SequenceID;
-
- // return...
- return newInfo;
+ Level = level;
+ Logger = logger;
+ Message = message;
+ Exception = ex;
+ TimeStamp = LogManager.GetDateTime();
+ SequenceID = Interlocked.Increment(ref _globalSequenceId);
}
}
}
View
49 MetroLog/Objects/LogManager.cs
@@ -3,53 +3,47 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
-using MetroLog.Layouts;
using MetroLog.Targets;
namespace MetroLog
{
- public static class LogManager
+ internal class LogManager : ILogManager
{
- public static LoggingConfiguration DefaultConfiguration { get; set; }
+ public LoggingConfiguration DefaultConfiguration { get; private set; }
- private static Dictionary<string, Logger> Loggers { get; set; }
- private static readonly object _loggersLock = new object();
+ private readonly Dictionary<string, Logger> _loggers;
+ private readonly object _loggersLock = new object();
- internal const string DateTimeFormat = "dd/MMM/yyyy HH:mm:ss";
+ internal const string DateTimeFormat = "o";
- static LogManager()
+ public LogManager(LoggingConfiguration configuration = null)
{
- Reset();
+ _loggers = new Dictionary<string, Logger>(StringComparer.OrdinalIgnoreCase);
+
+ if (configuration == null)
+ {
+ // default logging config...
+ configuration = new LoggingConfiguration();
+ configuration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new DebugTarget());
+ }
+ DefaultConfiguration = configuration;
}
- public static Logger GetLogger(ILoggable loggable, LoggingConfiguration config)
+ public ILogger GetLogger<T>(LoggingConfiguration config = null)
{
- return GetLogger(loggable.GetType().Name, config);
+ return GetLogger(typeof(T).Name, config);
}
- public static Logger GetLogger(string name, LoggingConfiguration config = null)
+ public ILogger GetLogger(string name, LoggingConfiguration config = null)
{
lock (_loggersLock)
{
- if (!(Loggers.ContainsKey(name)))
- Loggers[name] = new Logger(name, config);
- return Loggers[name];
+ if (!(_loggers.ContainsKey(name)))
+ _loggers[name] = new Logger(name, config ?? DefaultConfiguration);
+ return _loggers[name];
}
}
- public static void Reset(LoggingConfiguration configuration = null)
- {
- Loggers = new Dictionary<string, Logger>(StringComparer.OrdinalIgnoreCase);
-
- if (configuration == null)
- {
- // default logging config...
- configuration = new LoggingConfiguration();
- configuration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new DebugTarget());
- }
- DefaultConfiguration = configuration;
- }
// logs problems with the framework to Debug...
internal static void LogInternal(string message, Exception ex)
@@ -62,7 +56,6 @@ internal static void LogInternal(string message, Exception ex)
internal static DateTimeOffset GetDateTime()
{
- // NLog has a high efficiency version of this...
return DateTimeOffset.UtcNow;
}
}
View
162 MetroLog/Objects/Logger.cs
@@ -3,101 +3,89 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
-using MetroLog.Targets;
namespace MetroLog
{
- public class Logger
+ internal class Logger : ILogger
{
- private string Name { get; set; }
- private LoggingConfiguration Configuration { get; set; }
+ public string Name { get; private set; }
+ private readonly LoggingConfiguration _configuration;
internal Logger(string name, LoggingConfiguration config)
{
- this.Name = name;
+ Name = name;
// add a target...
- this.Configuration = config;
+ _configuration = config;
}
- private LoggingConfiguration ResolvedConfiguration
- {
- get
- {
- // do we have one?
- if (this.Configuration != null)
- return this.Configuration;
- else
- return LogManager.DefaultConfiguration;
- }
- }
- public Task<LogWriteOperation[]> Trace(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> TraceAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Trace, message, ex);
+ return LogAsync(LogLevel.Trace, message, ex);
}
- public Task<LogWriteOperation[]> Trace(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> TraceAsync(string message, params object[] ps)
{
- return Log(LogLevel.Trace, message, ps);
+ return LogAsync(LogLevel.Trace, message, ps);
}
- public Task<LogWriteOperation[]> Debug(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> DebugAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Debug, message, ex);
+ return LogAsync(LogLevel.Debug, message, ex);
}
- public Task<LogWriteOperation[]> Debug(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> DebugAsync(string message, params object[] ps)
{
- return Log(LogLevel.Debug, message, ps);
+ return LogAsync(LogLevel.Debug, message, ps);
}
- public Task<LogWriteOperation[]> Info(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> InfoAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Info, message, ex);
+ return LogAsync(LogLevel.Info, message, ex);
}
- public Task<LogWriteOperation[]> Info(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> InfoAsync(string message, params object[] ps)
{
- return Log(LogLevel.Info, message, ps);
+ return LogAsync(LogLevel.Info, message, ps);
}
- public Task<LogWriteOperation[]> Warn(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> WarnAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Warn, message, ex);
+ return LogAsync(LogLevel.Warn, message, ex);
}
- public Task<LogWriteOperation[]> Warn(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> WarnAsync(string message, params object[] ps)
{
- return Log(LogLevel.Warn, message, ps);
+ return LogAsync(LogLevel.Warn, message, ps);
}
- public Task<LogWriteOperation[]> Error(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> ErrorAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Error, message, ex);
+ return LogAsync(LogLevel.Error, message, ex);
}
- public Task<LogWriteOperation[]> Error(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> ErrorAsync(string message, params object[] ps)
{
- return Log(LogLevel.Error, message, ps);
+ return LogAsync(LogLevel.Error, message, ps);
}
- public Task<LogWriteOperation[]> Fatal(string message, Exception ex = null)
+ internal Task<LogWriteOperation[]> FatalAsync(string message, Exception ex = null)
{
- return Log(LogLevel.Fatal, message, ex);
+ return LogAsync(LogLevel.Fatal, message, ex);
}
- public Task<LogWriteOperation[]> Fatal(string message, params string[] ps)
+ internal Task<LogWriteOperation[]> FatalAsync(string message, params object[] ps)
{
- return Log(LogLevel.Fatal, message, ps);
+ return LogAsync(LogLevel.Fatal, message, ps);
}
- public Task<LogWriteOperation[]> Log(LogLevel logLevel, string message, Exception ex)
+ internal Task<LogWriteOperation[]> LogAsync(LogLevel logLevel, string message, Exception ex)
{
return LogInternal(logLevel, message, null, ex, false);
}
- public Task<LogWriteOperation[]> Log(LogLevel logLevel, string message, params object[] ps)
+ internal Task<LogWriteOperation[]> LogAsync(LogLevel logLevel, string message, params object[] ps)
{
return LogInternal(logLevel, message, ps, null, true);
}
@@ -106,30 +94,28 @@ private Task<LogWriteOperation[]> LogInternal(LogLevel level, string message, ob
{
try
{
- var targets = this.ResolvedConfiguration.GetTargets(level);
+ var targets = _configuration.GetTargets(level);
if (!(targets.Any()))
- return null;
+ return Task.FromResult(new LogWriteOperation[] { });
// format?
if (doFormat)
message = string.Format(message, ps);
// create an event entry and pass it through...
- var entry = new LogEventInfo(level, this.Name, message, ex);
+ var entry = new LogEventInfo(level, Name, message, ex);
- // do the sync ones...
- var tasks = new List<Task<LogWriteOperation>>();
- foreach (var target in targets)
- tasks.Add(target.WriteAsync(entry));
+ var writeTasks = from target in targets
+ select target.WriteAsync(entry);
// group...
- var group = Task.WhenAll<LogWriteOperation>(tasks);
+ var group = Task.WhenAll(writeTasks);
return group;
}
catch (Exception logEx)
{
LogManager.LogInternal("Logging operation failed.", logEx);
- return Task.FromResult<LogWriteOperation[]>(new LogWriteOperation[] {});
+ return Task.FromResult(new LogWriteOperation[] {});
}
}
@@ -181,9 +167,79 @@ public bool IsFatalEnabled
}
}
+ public void Trace(string message, Exception ex = null)
+ {
+ TraceAsync(message, ex);
+ }
+
+ public void Trace(string message, params object[] ps)
+ {
+ TraceAsync(message, ps);
+ }
+
+ public void Debug(string message, Exception ex = null)
+ {
+ DebugAsync(message, ex);
+ }
+
+ public void Debug(string message, params object[] ps)
+ {
+ DebugAsync(message, ps);
+ }
+
+ public void Info(string message, Exception ex = null)
+ {
+ InfoAsync(message, ex);
+ }
+
+ public void Info(string message, params object[] ps)
+ {
+ InfoAsync(message, ps);
+ }
+
+ public void Warn(string message, Exception ex = null)
+ {
+ WarnAsync(message, ex);
+ }
+
+ public void Warn(string message, params object[] ps)
+ {
+ WarnAsync(message, ps);
+ }
+
+ public void Error(string message, Exception ex = null)
+ {
+ ErrorAsync(message, ex);
+ }
+
+ public void Error(string message, params object[] ps)
+ {
+ ErrorAsync(message, ps);
+ }
+
+ public void Fatal(string message, Exception ex = null)
+ {
+ FatalAsync(message, ex);
+ }
+
+ public void Fatal(string message, params object[] ps)
+ {
+ FatalAsync(message, ps);
+ }
+
+ public void Log(LogLevel logLevel, string message, Exception ex)
+ {
+ LogAsync(logLevel, message, ex);
+ }
+
+ public void Log(LogLevel logLevel, string message, params object[] ps)
+ {
+ LogAsync(logLevel, message, ps);
+ }
+
public bool IsEnabled(LogLevel level)
{
- return this.ResolvedConfiguration.GetTargets(level).Any();
+ return _configuration.GetTargets(level).Any();
}
}
}
View
14 MetroLog/Objects/LoggingConfiguration.cs
@@ -9,35 +9,35 @@ namespace MetroLog
{
public class LoggingConfiguration
{
- private List<TargetBinding> Bindings { get; set; }
- private object _bindingsLock = new object();
+ private readonly List<TargetBinding> _bindings;
+ private readonly object _bindingsLock = new object();
public LoggingConfiguration()
{
- this.Bindings = new List<TargetBinding>();
+ _bindings = new List<TargetBinding>();
}
public void AddTarget(LogLevel level, Target target)
{