ExtensionsUtil.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Microsoft.AspNetCore.Routing;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace EdjCase.JsonRpc.Router.Utilities
  6. {
  7. public static class TypeExtensions
  8. {
  9. /// <summary>
  10. /// Determines if the type is nullable
  11. /// </summary>
  12. /// <param name="type">Type of the object</param>
  13. /// <returns>True if the type is nullable, otherwise False</returns>
  14. public static bool IsNullableType(this Type type)
  15. {
  16. return !type.IsValueType || Nullable.GetUnderlyingType(type) != null;
  17. }
  18. }
  19. public static class RouteContextExtensions
  20. {
  21. public static void MarkAsHandled(this RouteContext context)
  22. {
  23. context.Handler = c => Task.FromResult(0);
  24. }
  25. }
  26. public static class LoggerExtensions
  27. {
  28. public static void LogException(this ILogger logger, Exception ex, string message = null)
  29. {
  30. //Log error ignores the exception for some reason
  31. if (message != null)
  32. {
  33. message = $"{message}{Environment.NewLine}{ex}";
  34. }
  35. else
  36. {
  37. message = $"{ex}";
  38. }
  39. logger.LogError(new EventId(), ex, message);
  40. }
  41. }
  42. }