Design Pattern Examples
Overview of object-oriented design patterns
Bridge_Logger.cs
Go to the documentation of this file.
1
5
6using System;
7
9{
15 internal class Logger : IDisposable
16 {
21 public enum LoggerTypes
22 {
26 ToNull,
27
31 ToFile,
32
37 }
38
47 public Logger(LoggerTypes loggerType, params string[] arguments)
48 {
49 _logger = null;
50 switch (loggerType)
51 {
52 case LoggerTypes.ToNull:
54 break;
55
56 case LoggerTypes.ToConsole:
58 break;
59
60 case LoggerTypes.ToFile:
61 {
62 if (arguments != null && arguments.Length >= 1)
63 {
64 _logger = FileLogger.CreateFileLogger(arguments[0]);
65 }
66 else
67 {
68 string msg = String.Format("A filename must be specified for the {0} logger type.", loggerType);
69 throw new ArgumentException(msg, "arguments");
70 }
71 }
72 break;
73
74 default:
75 {
76 string msg = String.Format("The logger type '{0}' is not recognized. Cannot construct a Logger.", loggerType);
77 throw new ArgumentException(msg, "loggerType");
78 }
79 }
80 }
81
82
87 public void LogTrace(string message)
88 {
89 if (_logger != null)
90 {
91 _logger.LogTrace(message);
92 }
93 }
94
99 public void LogInfo(string message)
100 {
101 if (_logger != null)
102 {
103 _logger.LogInfo(message);
104 }
105 }
106
111 public void LogError(string message)
112 {
113 if (_logger != null)
114 {
115 _logger.LogError(message);
116 }
117 }
118
119 #region IDisposable Members
120
124 void IDisposable.Dispose()
125 {
126 if (_logger != null)
127 {
128 if (_logger is IDisposable)
129 {
130 ((IDisposable)_logger).Dispose();
131 }
132 }
133 }
134
135 #endregion
136
140 private ILogger? _logger;
141 }
142
143}
Represents a logger that writes to the console window.
static ILogger CreateConsoleLogger()
Create an instance of a console logger, where logging goes to the console.
Represents a logger to a file.
static ILogger CreateFileLogger(string filename)
Create an instance of a file logger.
Represents the logger object to be used in the program.
void LogInfo(string message)
Log informational messages to the configured output.
LoggerTypes
A value passed to Logger.Logger() constructor to specify the type of logger to create.
@ ToConsole
Log to the console. No additional parameters.
@ ToNull
Log to nowhere, that is, throw out all logging. No additional parameters.
@ ToFile
Log to a file. One additional parameter: the name of the file to log to.
void LogTrace(string message)
Log trace messages to the configured output.
ILogger? _logger
The logger implementation represented by the ILogger interface.
Logger(LoggerTypes loggerType, params string[] arguments)
Constructor that takes a LoggerTypes value and optional arguments to create a new Logger class.
void LogError(string message)
Log error messages to the configured output.
Represents a logger that throws away anything sent its way.
static ILogger CreateNullLogger()
Create an instance of a null logger, a logger that doesn't do anything.
Represents an implementation of a logger object as call from the Logger class.
void LogError(string msg)
Log error messages to the configured output.
void LogTrace(string msg)
Log trace messages to the configured output.
void LogInfo(string msg)
Log informational messages to the configured output.
The namespace containing all Design Pattern Examples implemented in C#.