/// <summary> /// Write exception to file. /// </summary> /// <param name="ex">Exception to write.</param> /// <param name="maxFiles">Maximum files per same exception.</param> /// <param name="logsFolder">Folder where exception reports will be created.</param> /// <param name="writeAsHtml">True - write as HTML, false - write as TEXT.</param> public void WriteException(Exception ex, int maxFiles, string logsFolder, bool writeAsHtml) { var ev = WritingException; var le = new LogHelperEventArgs() { Exception = ex }; if (ev != null) { ev(this, le); } if (le.Cancel) { return; } var allowToReport = AllowReportExceptionToFile(ex); if (!allowToReport) { return; } // Must wrap into lock so that process won't attempt to delete/write same file twice from different threads. lock (WriteLock) { // Create file. var prefix = "FCE_" + ex.GetType().Name; var ext = writeAsHtml ? "htm" : "txt"; var di = new System.IO.DirectoryInfo(logsFolder); // Create folder if not exists. if (!di.Exists) { di.Create(); } // Get exception files ordered with oldest on top. var files = di.GetFiles(prefix + "*." + ext).OrderBy(x => x.CreationTime).ToArray(); // Remove excess files if necessary. if (maxFiles > 0 && files.Count() > 0 && files.Count() > maxFiles) { // Remove oldest file. files[0].Delete(); } var fileTime = HiResDateTime.Current.Now; var fileName = string.Format("{0}\\{1}_{2:yyyyMMdd_HHmmss.ffffff}.{3}", di.FullName, prefix, fileTime, ext); var fi = new System.IO.FileInfo(fileName); var content = writeAsHtml ? ExceptionInfo(ex, "") : ex.ToString(); System.IO.File.AppendAllText(fileName, content); } }
void _WriteFile(Exception ex, string subject, string body) { var ev = WritingException; var le = new LogHelperEventArgs() { Exception = ex }; if (ev != null) { ev(this, le); } if (le.Cancel) { return; } // Must wrap into lock so that process won't attempt to delete/write same file twice from different threads. lock (WriteLock) { // Create file. var prefix = "FCE_" + ex.GetType().Name; var ext = WriteAsHtml ? ".htm" : ".txt"; var di = new System.IO.DirectoryInfo(_OverrideLogFolder ?? DefaultLogsFolder); // Create folder if not exists. if (!di.Exists) { di.Create(); } // Get exception files ordered with oldest on top. var files = di.GetFiles(prefix + "*." + ext).OrderBy(x => x.CreationTime).ToArray(); // Remove excess files if necessary. if (MaxFiles > 0 && files.Length > 0 && files.Length > MaxFiles) { // Remove oldest file. files[0].Delete(); } #if NETSTANDARD var fileTime = DateTime.Now; #else var fileTime = HiResDateTime.Current.Now; #endif var fileName = string.Format("{0}\\{1}_{2:yyyyMMdd_HHmmss.ffffff}{3}", di.FullName, prefix, fileTime, ext); var fi = new System.IO.FileInfo(fileName); var content = WriteAsHtml ? ExceptionInfo(ex, body) : ex.ToString(); System.IO.File.AppendAllText(fileName, content); } }
/// <summary> /// Write exception details to file. /// </summary> /// <param name="ex">Exception to write.</param> /// <param name="subject">Use custom subject instead of generated from exception</param> public void WriteException(Exception ex, string subject = null, string body = null) { // Check if exception can be ignored. var le = new LogHelperEventArgs() { Exception = ex }; WritingException?.Invoke(this, le); // If Exception reporting was cancelled then return. if (le.Cancel) { return; } Interlocked.Increment(ref ExceptionsCount); if (!LogToFile) { return; } _GroupException(fileExceptions, ex, subject, body, _WriteFile); }
public void WriteException(Exception ex, int maxFiles, string logsFolder, bool writeAsHtml) { var ev = WritingException; var le = new LogHelperEventArgs() { Exception = ex }; if (ev != null) { ev(this, le); } if (le.Cancel) { return; } // Must wrap into lock so that process won't attempt to delete/write same file twice from different threads. lock (WriteLock) { var n = DateTime.Now; var type = ex.GetType(); if (exeptionTimes.ContainsKey(type)) { // Increase counter. exeptionCount[type] = exeptionCount[type] + 1; // Do not allow write if not enough time passed. if (n.Subtract(exeptionTimes[type]).Milliseconds < 500) { return; } } else { exeptionTimes.Add(type, n); exeptionCount.Add(type, 1); } var count = exeptionCount[type]; // Reset count and update last write time. exeptionCount[type] = 0; exeptionTimes[type] = n; // Create file. var prefix = "FCE_" + ex.GetType().Name; var ext = writeAsHtml ? "htm" : "txt"; var di = new System.IO.DirectoryInfo(logsFolder); if (di.Exists) { var files = di.GetFiles(prefix + "*." + ext).OrderBy(x => x.CreationTime).ToArray(); if (maxFiles > 0 && files.Count() > 0 && files.Count() > maxFiles) { // Remove oldest file. files[0].Delete(); } } else { di.Create(); } //var fileTime = JocysCom.ClassLibrary.HiResDateTime.Current.Now; var fileTime = HiResDateTime.Current.Now; var fileName = string.Format("{0}\\{1}_{2:yyyyMMdd_HHmmss.ffffff}{3}.{4}", di.FullName, prefix, fileTime, count == 1 ? "" : "." + count.ToString(), ext); var fi = new System.IO.FileInfo(fileName); var content = writeAsHtml ? ExceptionInfo(ex, "") : ex.ToString(); System.IO.File.AppendAllText(fileName, content); } }