internal override async Task WriteException() { List <LogModelException> items = new List <LogModelException>(); try { while (!dataExceptionItems.IsCompleted && dataExceptionItems.Count > 0 && items.Count < dataExceptionItems.BoundedCapacity) { items.Add(dataExceptionItems.Take()); } if (items.Any()) { DateTime logTime = DateTime.Now.AddHours(AdditinalHour); string path = Path.Combine(logPath, logName) + " " + logTime.ToString("yyyy-MM-dd") + " Exception.xml"; if (File.Exists(path)) { LogModelException item = items[0]; XDocument xDocument = XDocument.Load(path, LoadOptions.PreserveWhitespace); XElement root = xDocument.Element(logName); XElement currentparent = new XElement("Exception", new XElement("Caption", item.Caption), new XElement("Data", item.Data), new XElement("FileName", item.FileName), new XElement("Method", item.Method), new XElement("LineNumber", item.LineNumber) ); currentparent.SetAttributeValue("Time", item.StringTime); root.AddFirst(currentparent); xDocument.Save(path); WriteItemList(path, items.Skip(1)); } else { WriteItemList(path, items); } } } catch (Exception ex) { } finally { items.Clear(); items = null; await Task.Delay(30); } }
/// <summary> /// Log exception with filename, method and line number. /// </summary> /// <param name="e">The exception to be logged</param> /// <param name="caption">If you want to add title message for this exception</param> /// <param name="ExceptionFinalMessage">Delegate on final exception message</param> public virtual void LogException(Exception e, string caption = "", Action <string> ExceptionFinalMessage = null) { Task.Run(() => { try { LogModelException model = new LogModelException() { Time = DateTime.Now.AddHours(AdditinalHour), }; StringBuilder message = new StringBuilder(); Exception temp = e; do { message.Append(temp.Message); if (temp.InnerException == null) { // Create a StackDebug that captures // filename, line number, and column // information for the current thread. StackTrace st = new StackTrace(temp, true); if (st.FrameCount == 0) { model.FileName = ""; model.Method = ""; model.LineNumber = -1; } else { StackFrame sf = st.GetFrame(0); model.FileName = string.Join(" => ", sf.GetFileName().Split('\\').Skip(2)); model.Method = sf.GetMethod().Name; model.LineNumber = sf.GetFileLineNumber(); } break; } temp = temp.InnerException; } while (temp != null); model.Data = message.ToString(); model.Caption = caption; ExceptionFinalMessage?.Invoke(model.Data); dataExceptionItems.Add(model); } catch { } }); }
internal override async Task WriteException() { List <string> items = new List <string>(); try { while (!dataExceptionItems.IsCompleted && dataExceptionItems.Count > 0 && items.Count < dataExceptionItems.BoundedCapacity) { LogModelException item = dataExceptionItems.Take(); items.Add($"StringTime: {item.StringTime} |=> Caption: {item.Caption} |=> Message: {item.Data} |=> FileName: {item.FileName} |=> Method: {item.Method} |=> LineNumber: {item.LineNumber}"); } WritetoDisk(items, "Exception"); } finally { items.Clear(); items = null; await Task.Delay(30); } }
internal override async Task WriteException() { try { Table.Rows.Clear(); while (!dataExceptionItems.IsCompleted && dataExceptionItems.Count > 0 && Table.Rows.Count < dataExceptionItems.BoundedCapacity) { LogModelException item = dataExceptionItems.Take(); Table.Rows.Add(0, item.Time, item.Data, item.LineNumber, item.FileName, item.Method, item.Caption); } using (SqlBulkCopy bulkCopy = new SqlBulkCopy(logConnection)) { bulkCopy.DestinationTableName = "dbo." + logName + "_Exception"; await bulkCopy.WriteToServerAsync(Table); } } finally { await Task.Delay(30); } }