PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Globalization;
  19. using System.IO;
  20. using Microsoft.Practices.Composite.Properties;
  21. namespace Microsoft.Practices.Composite.Logging
  22. {
  23. /// <summary>
  24. /// Implementation of <see cref="ILoggerFacade"/> that logs into a <see cref="TextWriter"/>.
  25. /// </summary>
  26. public class TextLogger : ILoggerFacade, IDisposable
  27. {
  28. private readonly TextWriter writer;
  29. /// <summary>
  30. /// Initializes a new instance of <see cref="TextLogger"/> that writes to
  31. /// the console output.
  32. /// </summary>
  33. public TextLogger()
  34. : this(Console.Out)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of <see cref="TextLogger"/>.
  39. /// </summary>
  40. /// <param name="writer">The writer to use for writing log entries.</param>
  41. public TextLogger(TextWriter writer)
  42. {
  43. if (writer == null)
  44. throw new ArgumentNullException("writer");
  45. this.writer = writer;
  46. }
  47. /// <summary>
  48. /// Write a new log entry with the specified category and priority.
  49. /// </summary>
  50. /// <param name="message">Message body to log.</param>
  51. /// <param name="category">Category of the entry.</param>
  52. /// <param name="priority">The priority of the entry.</param>
  53. public void Log(string message, Category category, Priority priority)
  54. {
  55. string messageToLog = String.Format(CultureInfo.InvariantCulture, Resources.DefaultTextLoggerPattern, DateTime.Now,
  56. category.ToString().ToUpper(CultureInfo.InvariantCulture), message, priority.ToString());
  57. writer.WriteLine(messageToLog);
  58. }
  59. /// <summary>
  60. /// Disposes the associated <see cref="TextWriter"/>.
  61. /// </summary>
  62. /// <param name="disposing">When <see langword="true"/>, disposes the associated <see cref="TextWriter"/>.</param>
  63. protected virtual void Dispose(bool disposing)
  64. {
  65. if (disposing)
  66. {
  67. if (writer != null)
  68. {
  69. writer.Dispose();
  70. }
  71. }
  72. }
  73. ///<summary>
  74. ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  75. ///</summary>
  76. /// <remarks>Calls <see cref="Dispose(bool)"/></remarks>.
  77. ///<filterpriority>2</filterpriority>
  78. public void Dispose()
  79. {
  80. Dispose(true);
  81. GC.SuppressFinalize(this);
  82. }
  83. }
  84. }