Permalink
Browse files

logging abstraction layer

  • Loading branch information...
rcd committed Mar 13, 2013
1 parent 4b0f0f8 commit 7f5fee3240354ee910d003f5b8a313fd6bea993f
View
@@ -30,17 +30,6 @@ class Program {
Console.ForegroundColor = old;
};
- LoggingConfiguration config = new LoggingConfiguration();
-
- ColoredConsoleTarget ct = new ColoredConsoleTarget();
- ct.Layout = "${message}";
- config.AddTarget("Console", ct);
-
- config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, ct));
-
- LogManager.Configuration = config;
-
-
//var server = new DicomServer<DicomCEchoProvider>(12345);
View
@@ -68,6 +68,11 @@
<Compile Include="DicomDictionaryEntry.cs" />
<Compile Include="DicomDictionaryReader.cs" />
<Compile Include="Imaging\Mathematics\MovingAverage.cs" />
+ <Compile Include="Log\ConsoleLogger.cs" />
+ <Compile Include="Log\Logger.cs" />
+ <Compile Include="Log\LogLevel.cs" />
+ <Compile Include="Log\LogManager.cs" />
+ <Compile Include="Log\NLogManager.cs" />
<Compile Include="Media\DicomFileScanner.cs" />
<Compile Include="DicomMatchRules.cs" />
<Compile Include="DicomTransformRules.cs" />
@@ -268,7 +273,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
- <UserProperties BuildVersion_UseGlobalSettings="True" BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" />
+ <UserProperties BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" BuildVersion_UseGlobalSettings="True" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -1,11 +1,11 @@
using System;
-using NLog;
+using Dicom.Log;
namespace Dicom.Imaging.Codec {
public class DicomCodecParams {
protected DicomCodecParams() {
- Logger = LogManager.GetLogger("Dicom.Imaging.Codec");
+ Logger = LogManager.Default.GetLogger("Dicom.Imaging.Codec");
}
public Logger Logger {
@@ -4,11 +4,10 @@
using System.Reflection;
using System.ComponentModel.Composition.Hosting;
-using NLog;
-
using Dicom.IO;
using Dicom.IO.Buffer;
using Dicom.IO.Writer;
+using Dicom.Log;
namespace Dicom.Imaging.Codec {
public class DicomTranscoder {
@@ -30,7 +29,7 @@ public class DicomTranscoder {
if (path == null)
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- var log = LogManager.GetLogger("Dicom.Imaging.Codec");
+ var log = LogManager.Default.GetLogger("Dicom.Imaging.Codec");
var catalog = (search == null) ?
new DirectoryCatalog(path) :
View
@@ -0,0 +1,44 @@
+using System;
+
+namespace Dicom.Log {
+ public class ConsoleLogger : Logger {
+ public readonly static Logger Instance = new ConsoleLogger();
+ private object _lock = new object();
+
+ private ConsoleLogger() {
+ }
+
+ public override void Log(LogLevel level, string msg, params object[] args) {
+ lock (_lock) {
+ var previous = Console.ForegroundColor;
+ switch (level) {
+ case LogLevel.Debug:
+ Console.ForegroundColor = ConsoleColor.Blue;
+ break;
+ case LogLevel.Info:
+ Console.ForegroundColor = ConsoleColor.White;
+ break;
+ case LogLevel.Warning:
+ Console.ForegroundColor = ConsoleColor.Yellow;
+ break;
+ case LogLevel.Error:
+ Console.ForegroundColor = ConsoleColor.Red;
+ break;
+ case LogLevel.Fatal:
+ Console.ForegroundColor = ConsoleColor.Magenta;
+ break;
+ default:
+ break;
+ }
+ Console.WriteLine(msg, args);
+ Console.ForegroundColor = previous;
+ }
+ }
+ }
+
+ public class ConsoleLogManager : LogManager {
+ public override Logger GetLogger(string name) {
+ return ConsoleLogger.Instance;
+ }
+ }
+}
@@ -1,9 +1,8 @@
using System;
using System.Text;
-using NLog;
-
using Dicom.IO.Buffer;
+using Dicom.Log;
namespace Dicom.Log {
public class DicomDatasetLogger : IDicomDatasetWalker {
@@ -4,10 +4,9 @@
using System.Text;
using Dicom.IO;
-using NLog;
-
using Dicom.IO.Buffer;
using Dicom.IO.Reader;
+using Dicom.Log;
namespace Dicom.Log {
public class DicomParserLogger : IDicomReaderObserver {
View
@@ -3,8 +3,6 @@
using System.Linq;
using System.Text;
-using NLog;
-
namespace Dicom.Log {
public static class Extensions {
public static void WriteToLog(this IEnumerable<DicomItem> dataset, Logger log, LogLevel level) {
View
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Dicom.Log {
+ public enum LogLevel {
+ Debug,
+ Info,
+ Warning,
+ Error,
+ Fatal
+ }
+}
View
@@ -0,0 +1,16 @@
+using System;
+
+namespace Dicom.Log {
+ public abstract class LogManager {
+ static LogManager() {
+ Default = new ConsoleLogManager();
+ }
+
+ public static LogManager Default {
+ get;
+ set;
+ }
+
+ public abstract Logger GetLogger(string name);
+ }
+}
View
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Dicom.Log {
+ public abstract class Logger {
+ public abstract void Log(LogLevel level, string msg, params object[] args);
+
+ public void Debug(string msg, params object[] args) {
+ Log(LogLevel.Debug, msg, args);
+ }
+
+ public void Info(string msg, params object[] args) {
+ Log(LogLevel.Info, msg, args);
+ }
+
+ public void Warn(string msg, params object[] args) {
+ Log(LogLevel.Warning, msg, args);
+ }
+
+ public void Error(string msg, params object[] args) {
+ Log(LogLevel.Error, msg, args);
+ }
+
+ public void Fatal(string msg, params object[] args) {
+ Log(LogLevel.Fatal, msg, args);
+ }
+ }
+}
View
@@ -0,0 +1,46 @@
+using System;
+
+namespace Dicom.Log {
+ /// <summary>
+ /// LogManager for the NLog logging framework.
+ /// </summary>
+ /// <example>
+ /// LogManager.Default = new NLogManager();
+ /// </example>
+ public class NLogManager : LogManager {
+ public override Logger GetLogger(string name) {
+ return new NLogger(NLog.LogManager.GetLogger(name));
+ }
+
+ private class NLogger : Logger {
+ private NLog.Logger _logger;
+
+ public NLogger(NLog.Logger logger) {
+ _logger = logger;
+ }
+
+ public override void Log(LogLevel level, string msg, params object[] args) {
+ switch (level) {
+ case LogLevel.Debug:
+ _logger.Debug(msg, args);
+ break;
+ case LogLevel.Info:
+ _logger.Info(msg, args);
+ break;
+ case LogLevel.Warning:
+ _logger.Warn(msg, args);
+ break;
+ case LogLevel.Error:
+ _logger.Error(msg, args);
+ break;
+ case LogLevel.Fatal:
+ _logger.Fatal(msg, args);
+ break;
+ default:
+ _logger.Info(msg, args);
+ break;
+ }
+ }
+ }
+ }
+}
@@ -1,7 +1,7 @@
using System;
using System.IO;
-using NLog;
+using Dicom.Log;
namespace Dicom.Network {
public class DicomCEchoProvider : DicomService, IDicomServiceProvider, IDicomCEchoProvider {
@@ -8,7 +8,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
-using NLog;
+using Dicom.Log;
namespace Dicom.Network {
public class DicomClient {
@@ -8,7 +8,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
-using NLog;
+using Dicom.Log;
namespace Dicom.Network {
public class DicomServer<T> : IDisposable where T: DicomService, IDicomServiceProvider {
@@ -75,7 +75,7 @@ public class DicomServer<T> : IDisposable where T: DicomService, IDicomServicePr
_clients.Add(scp);
} catch (Exception e) {
if (Logger == null)
- Logger = LogManager.GetLogger("Dicom.Network");
+ Logger = LogManager.Default.GetLogger("Dicom.Network");
Logger.Error("Exception accepting client: " + e.ToString());
} finally {
if (_listener != null)
Oops, something went wrong.

0 comments on commit 7f5fee3

Please sign in to comment.