Make printToStackTrace Extension method
public static void PrintToStackTrace(this Exception exception)
{
try
{
using (var fileStream = new FileStream($"{LogPath}Log.log", FileMode.Append, FileAccess.Write))
{
var encoder = Encoding.Default.GetEncoder();
var stackTrace = exception.StackTrace;
var text = string.Format("=================={0}{1}{0}{4}{0}{2}{0}{3}{0}{5}{0}=================={0}", Environment.NewLine, DateTime.Now, stackTrace, exception, exception.Message, exception.InnerException);
var data = new byte[text.Length];
var output = text.ToCharArray();
encoder.GetBytes(output, 0, output.Length, data, 0, true);
fileStream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
usage
ex.PrinttoStackTrace();
{
try
{
using (var fileStream = new FileStream($"{LogPath}Log.log", FileMode.Append, FileAccess.Write))
{
var encoder = Encoding.Default.GetEncoder();
var stackTrace = exception.StackTrace;
var text = string.Format("=================={0}{1}{0}{4}{0}{2}{0}{3}{0}{5}{0}=================={0}", Environment.NewLine, DateTime.Now, stackTrace, exception, exception.Message, exception.InnerException);
var data = new byte[text.Length];
var output = text.ToCharArray();
encoder.GetBytes(output, 0, output.Length, data, 0, true);
fileStream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
usage
ex.PrinttoStackTrace();
No comments: