using System; using System.Globalization; using System.Runtime.CompilerServices; namespace Infrastructure.Extensions { public static class ExceptionExtensions { public static void PrintStack(this Exception ex, string rawMessage = null, [CallerMemberName] string memberName = "") { if (ex is null) { throw new ArgumentNullException(nameof(ex)); } Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff", CultureInfo.CurrentCulture)); if (rawMessage != null) { Console.WriteLine(rawMessage); } Console.WriteLine(memberName); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); ex.InnerException?.PrintStack(); } public static string GetMessage(this Exception ex) { return ex?.InnerException?.Message ?? ex.Message; } } }