PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite/Logging/TextLogger.cs

#
C# | 92 lines | 42 code | 8 blank | 42 comment | 5 complexity | 8e3d9c527f9f6a45fd7bf0d9885918b9 MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System;
18using System.Globalization;
19using System.IO;
20using Microsoft.Practices.Composite.Properties;
21
22namespace Microsoft.Practices.Composite.Logging
23{
24    /// <summary>
25    /// Implementation of <see cref="ILoggerFacade"/> that logs into a <see cref="TextWriter"/>.
26    /// </summary>
27    public class TextLogger : ILoggerFacade, IDisposable
28    {
29        private readonly TextWriter writer;
30
31        /// <summary>
32        /// Initializes a new instance of <see cref="TextLogger"/> that writes to
33        /// the console output.
34        /// </summary>
35        public TextLogger()
36            : this(Console.Out)
37        {
38        }
39
40        /// <summary>
41        /// Initializes a new instance of <see cref="TextLogger"/>.
42        /// </summary>
43        /// <param name="writer">The writer to use for writing log entries.</param>
44        public TextLogger(TextWriter writer)
45        {
46            if (writer == null)
47                throw new ArgumentNullException("writer");
48
49            this.writer = writer;
50        }
51
52        /// <summary>
53        /// Write a new log entry with the specified category and priority.
54        /// </summary>
55        /// <param name="message">Message body to log.</param>
56        /// <param name="category">Category of the entry.</param>
57        /// <param name="priority">The priority of the entry.</param>
58        public void Log(string message, Category category, Priority priority)
59        {
60            string messageToLog = String.Format(CultureInfo.InvariantCulture, Resources.DefaultTextLoggerPattern, DateTime.Now,
61                                                category.ToString().ToUpper(CultureInfo.InvariantCulture), message, priority.ToString());
62
63            writer.WriteLine(messageToLog);
64        }
65
66        /// <summary>
67        /// Disposes the associated <see cref="TextWriter"/>.
68        /// </summary>
69        /// <param name="disposing">When <see langword="true"/>, disposes the associated <see cref="TextWriter"/>.</param>
70        protected virtual void Dispose(bool disposing)
71        {
72            if (disposing)
73            {
74                if (writer != null)
75                {
76                    writer.Dispose();
77                }
78            }
79        }
80
81        ///<summary>
82        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
83        ///</summary>
84        /// <remarks>Calls <see cref="Dispose(bool)"/></remarks>.
85        ///<filterpriority>2</filterpriority>
86        public void Dispose()
87        {
88            Dispose(true);
89            GC.SuppressFinalize(this);
90        }
91    }
92}