Design Pattern Examples
Overview of object-oriented design patterns
Bridge_FIleLogger.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections.Generic;
8using System.IO;
9using System.Linq;
10using System.Text;
11using System.Threading.Tasks;
12
14{
18 internal class FileLogger : ILogger, IDisposable
19 {
20 StreamWriter _outputFile;
22
40 public FileLogger(string filename)
41 {
42 _outputFile = File.CreateText(filename);
43 }
44
45
51 private void _WriteLine(string logLevel, string msg)
52 {
53 string output = LoggerHelpers.FormatLogLine(logLevel, msg);
54 _outputFile.WriteLine(output);
55 }
56
57 #region ILogger Members
58
59 void ILogger.LogTrace(string msg)
60 {
61 _WriteLine("TRACE", msg);
62 }
63
64 void ILogger.LogInfo(string msg)
65 {
66 _WriteLine("INFO ", msg);
67 }
68
69 void ILogger.LogError(string msg)
70 {
71 _WriteLine("ERROR", msg);
72 }
73
74 #endregion
75
76 #region IDisposable Members
77
81 void IDisposable.Dispose()
82 {
83 if (!_disposed)
84 {
85 _outputFile.Close();
86 _disposed = true;
87 }
88 }
89
90 #endregion
91
92
98 public static ILogger CreateFileLogger(string filename)
99 {
100 return new FileLogger(filename);
101 }
102 }
103}
Represents a logger to a file.
static ILogger CreateFileLogger(string filename)
Create an instance of a file logger.
FileLogger(string filename)
Constructor. If successful, the file is opened for writing. Raises an exception if the output file ca...
void _WriteLine(string logLevel, string msg)
Write a formatted line to the log.
static string FormatLogLine(string logLevel, string msg)
Format a line for logging, including time stamp.
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#.