SubtitleEdit/libse/SeLogger.cs

67 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Globalization;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
public static class SeLogger
{
public static void Error(Exception exception, string message = null)
{
if (exception == null)
2019-01-19 14:40:37 +01:00
{
return;
2019-01-19 14:40:37 +01:00
}
try
{
string filePath = Path.Combine(Configuration.DataDirectory, "error_log.txt");
using (var writer = new StreamWriter(filePath, true, Encoding.UTF8))
{
writer.WriteLine("-----------------------------------------------------------------------------");
writer.WriteLine("Date: " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
if (!string.IsNullOrWhiteSpace(message))
2019-01-19 14:40:37 +01:00
{
writer.WriteLine("Message: " + message);
2019-01-19 14:40:37 +01:00
}
writer.WriteLine();
// write exception + all inner exceptions
var ex = exception;
while (ex != null)
{
writer.WriteLine(ex.GetType().FullName);
writer.WriteLine("Message: " + ex.Message);
writer.WriteLine("StackTrace: " + ex.StackTrace);
ex = ex.InnerException;
}
}
}
catch
{
throw exception;
}
}
public static void Error(string message)
{
string filePath = Path.Combine(Configuration.DataDirectory, "error_log.txt");
using (var writer = new StreamWriter(filePath, true, Encoding.UTF8))
{
writer.WriteLine("-----------------------------------------------------------------------------");
writer.WriteLine("Date: " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
if (!string.IsNullOrWhiteSpace(message))
{
writer.WriteLine("Message: " + message);
}
writer.WriteLine();
}
}
}
}