public static void LogEvent(string details)
{
StackTrace st = new StackTrace();
// Putting into the string with only current program stack trace
string stack = st.ToString().Substring(0, st.ToString().IndexOf(" at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)") - 2);
stack = stack.Replace("at", "");
stack = stack.Replace(" ", "");
stack = stack.Replace("\r\n", "|");
// Reversing
string[] all = stack.Split('|');
Array.Reverse(all);
stack = "";
bool first = true;
foreach (var s in all)
{
if (first == false)
{
stack += "->";
}
else
{
first = false;
}
stack += s;
}
string message = "";
string currentDate = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.GetCultureInfo("en-US"));
string currentTime = DateTime.Now.ToString("HH:mm:ss");
message = currentDate + " " + currentTime + " [" + stack + "] " + details;
StreamWriter writer = null;
try
{
writer = OpenFile(DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.GetCultureInfo("en-US")));
}
catch (Exception e)
{
writer = OpenFile(DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.GetCultureInfo("en-US")) + "ERROR");
message = e.Message;
}
try
{
writer.WriteLine(message);
}
catch (Exception e)
{
writer.Close();
writer = new StreamWriter("ERROR.log", true);
writer.WriteLine(message);
}
CloseFile(writer);
}