您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Logger.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.InputError)
  28. return TraceEventType.Warning;
  29. if (status == ResultStatus.NotFound)
  30. return TraceEventType.Information;
  31. return TraceEventType.Verbose;
  32. }
  33. /// <summary>
  34. /// Config the logger for database and default settings provider
  35. /// </summary>
  36. public static void Config()
  37. {
  38. DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
  39. IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
  40. LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
  41. Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(logWriterFactory.Create());
  42. }
  43. /// <summary>
  44. /// Log provided informations
  45. /// </summary>
  46. /// <param name="status">The status to be logged</param>
  47. /// <param name="title">The log title</param>
  48. /// <param name="message">The log message</param>
  49. public static void Log(ResultStatus status, string title, string message)
  50. {
  51. try
  52. {
  53. Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(new LogEntry()
  54. {
  55. Title = title,
  56. Severity = ResultStatusToSeverity(status),
  57. Message = message
  58. });
  59. }
  60. catch (Exception e)
  61. {
  62. }
  63. }
  64. public static OpResult<T> LogOpResult<T>(OpResult<T> result)
  65. {
  66. string message = "Location:\n";
  67. for (int i = 0; i < result.StackFrames.Length && i < 10; ++i)
  68. {
  69. var frame = result.StackFrames[i];
  70. var method = frame.GetMethod();
  71. if (method.DeclaringType != null)
  72. message += method.DeclaringType.Name + "." + frame.GetMethod().Name + "\n";
  73. }
  74. if (result.PrivateDetails != null)
  75. message += "\nPrivate details:\n" + result.PrivateDetails + "\n";
  76. if (result.PublicDetails != null)
  77. message += "\nPublic details:\n" + result.PublicDetails;
  78. Log(result.Status, result.Status.ToString(), message);
  79. return result;
  80. }
  81. /// <summary>
  82. /// Create and returns a new OpResult with
  83. /// PrivateDetails = privateDetails, Data = null, Status = status
  84. /// If publicDetails == "", privateDetails will be used
  85. /// Used for all errors (invalid input, ...)
  86. /// </summary>
  87. public static OpResult<T> Error<T>(ResultStatus status, string privateDetails, string publicDetails = null)
  88. {
  89. return LogOpResult(OpResult<T>.Error(status, privateDetails, publicDetails));
  90. }
  91. /// <summary>
  92. /// Create and returns a new OpResult with
  93. /// PrivateDetails = e to string, Data = null, Status = status
  94. /// If publicDetails == "", privateDetails will be used
  95. /// Used for all errors (invalid input, ...)
  96. /// </summary>
  97. public static OpResult<T> Error<T>(ResultStatus status, Exception e, string userDetails = null)
  98. {
  99. return LogOpResult(OpResult<T>.Error(status, e, userDetails));
  100. }
  101. }
  102. }