Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Logger.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Diagnostics;
  3. using iiie.Logs.DBO;
  4. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
  5. using Microsoft.Practices.EnterpriseLibrary.Data;
  6. using Microsoft.Practices.EnterpriseLibrary.Logging;
  7. namespace iiie.Logs.DataAccess
  8. {
  9. /// <summary>
  10. /// Logger for OpResult
  11. /// </summary>
  12. public static class Logger
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="status"></param>
  18. /// <returns></returns>
  19. public static TraceEventType ResultStatusToSeverity(ResultStatus status)
  20. {
  21. if (status == ResultStatus.DBError)
  22. return TraceEventType.Critical;
  23. if (status == ResultStatus.LoginError)
  24. return TraceEventType.Error;
  25. if (status == ResultStatus.PermissionError)
  26. return TraceEventType.Error;
  27. if (status == ResultStatus.InternalError)
  28. return TraceEventType.Error;
  29. if (status == ResultStatus.InputError)
  30. return TraceEventType.Warning;
  31. if (status == ResultStatus.NotFound)
  32. return TraceEventType.Information;
  33. return TraceEventType.Verbose;
  34. }
  35. /// <summary>
  36. /// Config the logger for database and default settings provider
  37. /// </summary>
  38. public static void Config()
  39. {
  40. DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
  41. IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
  42. LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
  43. Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(logWriterFactory.Create());
  44. }
  45. /// <summary>
  46. /// Log provided informations
  47. /// </summary>
  48. /// <param name="status">The status to be logged</param>
  49. /// <param name="title">The log title</param>
  50. /// <param name="message">The log message</param>
  51. public static void Log(ResultStatus status, string title, string message)
  52. {
  53. try
  54. {
  55. Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(new LogEntry()
  56. {
  57. Title = title,
  58. Severity = ResultStatusToSeverity(status),
  59. Message = message
  60. });
  61. }
  62. catch (Exception e)
  63. {
  64. }
  65. }
  66. /// <summary>
  67. /// Log an exception
  68. /// </summary>
  69. /// <param name="e">The exception to be logged</param>
  70. public static void Log(Exception e)
  71. {
  72. LogOpResult(OpResult<int>.Error(ResultStatus.InternalError, e));
  73. }
  74. /// <summary>
  75. /// Log an OpResult
  76. /// </summary>
  77. /// <typeparam name="T">User data type</typeparam>
  78. /// <param name="result">The result to be logged</param>
  79. /// <returns>The OpResult</returns>
  80. public static OpResult<T> LogOpResult<T>(OpResult<T> result)
  81. {
  82. string message = "Location:\n";
  83. for (int i = 0; i < result.StackFrames.Length && i < 10; ++i)
  84. {
  85. var frame = result.StackFrames[i];
  86. var method = frame.GetMethod();
  87. if (method.DeclaringType != null)
  88. message += method.DeclaringType.Name + "." + frame.GetMethod().Name + "\n";
  89. }
  90. if (result.PrivateDetails != null)
  91. message += "\nPrivate details:\n" + result.PrivateDetails + "\n";
  92. if (result.PublicDetails != null)
  93. message += "\nPublic details:\n" + result.PublicDetails;
  94. Log(result.Status, result.Status.ToString(), message);
  95. return result;
  96. }
  97. /// <summary>
  98. /// Create and returns a new OpResult with
  99. /// PrivateDetails = privateDetails, Data = null, Status = status
  100. /// If publicDetails == "", privateDetails will be used
  101. /// Used for all errors (invalid input, ...)
  102. /// </summary>
  103. public static OpResult<T> Error<T>(ResultStatus status, string privateDetails, string publicDetails = null)
  104. {
  105. return LogOpResult(OpResult<T>.Error(status, privateDetails, publicDetails));
  106. }
  107. /// <summary>
  108. /// Create and returns a new OpResult with
  109. /// PrivateDetails = e to string, Data = null, Status = status
  110. /// If publicDetails == "", privateDetails will be used
  111. /// Used for all errors (invalid input, ...)
  112. /// </summary>
  113. public static OpResult<T> Error<T>(ResultStatus status, Exception e, string userDetails = null)
  114. {
  115. return LogOpResult(OpResult<T>.Error(status, e, userDetails));
  116. }
  117. }
  118. }