PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkEmailFomatter/Utilities/Extensions/LogExt.cs

https://bitbucket.org/nicdao/frg-think-emailformatter
C# | 90 lines | 75 code | 9 blank | 6 comment | 5 complexity | 0612d76cb602c65600ed89016e423e68 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using ThinkEmailFormatter.Models;
  9. using System.Web.Mvc;
  10. using Microsoft.Practices.EnterpriseLibrary.Logging;
  11. using System.Diagnostics;
  12. using System.Text;
  13. namespace ThinkEmailFormatter.Models.Extensions
  14. {
  15. public static class LogExt
  16. {
  17. public static void Warning(this LogWriter log, string message)
  18. {
  19. if (log.IsLoggingEnabled())
  20. {
  21. LogEntry logEntry = new LogEntry()
  22. {
  23. Message = message,
  24. Severity = TraceEventType.Warning
  25. };
  26. log.Write(logEntry);
  27. }
  28. }
  29. public static void Error(this LogWriter log, string message)
  30. {
  31. if (log.IsLoggingEnabled())
  32. {
  33. LogEntry logEntry = new LogEntry()
  34. {
  35. Message = message,
  36. Severity = TraceEventType.Error
  37. };
  38. log.Write(logEntry);
  39. }
  40. }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. /// <param name="log"></param>
  45. /// <param name="ie"></param>
  46. /// <param name="location">Optional parameter that should provide where the exception was thrown(e.g. class.method)</param>
  47. public static void Exception(this LogWriter log, Exception ie, string location = null)
  48. {
  49. if (log.IsLoggingEnabled())
  50. {
  51. StringBuilder fullMessage = new StringBuilder(Environment.NewLine);
  52. if (string.IsNullOrEmpty(location))
  53. fullMessage.AppendFormat(" An exception was thrown: {0}", ie.Message);
  54. else
  55. {
  56. fullMessage.AppendFormat(" An exception was thrown in {0}.", location);
  57. fullMessage.Append(Environment.NewLine);
  58. fullMessage.AppendFormat(" Details: {0}", ie.Message);
  59. }
  60. LogEntry logEntry = new LogEntry()
  61. {
  62. Message = fullMessage.ToString(),
  63. Severity = TraceEventType.Error
  64. };
  65. log.Write(logEntry);
  66. }
  67. }
  68. public static void Information(this LogWriter log, string message)
  69. {
  70. if (log.IsLoggingEnabled())
  71. {
  72. LogEntry logEntry = new LogEntry()
  73. {
  74. Message = message,
  75. Severity = TraceEventType.Information
  76. };
  77. log.Write(logEntry);
  78. }
  79. }
  80. }
  81. }