public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data) { var logEntry = (Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry)data; var fileLogEntry = new LogEntry { TimeStamp = logEntry.TimeStamp.Add(TimeZoneAdjustment), ApplicationDomainId = AppDomain.CurrentDomain.Id, ThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId, Message = logEntry.Message, Severity = logEntry.Severity.ToString(), }; string title = logEntry.Title; title = title.Substring(title.IndexOf(')') + 2); // Removing ({AppDomainId} - {ThreadId}}) prefix. // Extracting display options from title if (title.StartsWith("RGB(")) { fileLogEntry.DisplayOptions = title.Substring(0, title.IndexOf(')') + 1); title = title.Substring(fileLogEntry.DisplayOptions.Length); } fileLogEntry.Title = title; LoggerInstance.WriteEntry(fileLogEntry); }
public static LogEntry Parse(string serializedLogEntry) { Verify.ArgumentNotNull(serializedLogEntry, "serializedLogEntry"); if(serializedLogEntry.IndexOf((char)65533) == -1 && serializedLogEntry.IndexOf((char)160) == -1) { return null; } string[] parts = serializedLogEntry.Split((char)65533); if(parts.Length != 8) { parts = serializedLogEntry.Split((char)160); } if(parts.Length != 8) { return null; } var result = new LogEntry(); try { string date = parts[0] + parts[1]; DateTime timeStamp; if (!DateTime.TryParseExact(date, "yyyyMMddHH:mm:ss.ffff", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out timeStamp)) { return null; } result.TimeStamp = timeStamp; result.ApplicationDomainId = int.Parse(parts[2]); result.ThreadId = int.Parse(parts[3]); result.Severity = parts[4]; result.Title = parts[5]; result.DisplayOptions = parts[6]; result.Message = parts[7]; } catch(Exception) { return null; } return result; }
public void WriteEntry(LogEntry entry) { string logLine = entry.ToString(); byte[] bytes = Encoding.UTF8.GetBytes(logLine + "\n"); EnsureInitialize(); lock (_syncRoot) { FileConnection.NewEntries.Add(entry); // Checking whether we should change the file after midnight int dayNumber = entry.TimeStamp.Day; if (dayNumber != FileConnection.CreationDate.Day && dayNumber == DateTime.Now.Day) { ResetInitialization(); } // Writing the file in the "catch" block in order to prevent chance of corrupting the file by experiencing ThreadAbortException. Exception thrownException = null; try { } finally { try { FileConnection.FileStream.Write(bytes, 0, bytes.Length); if (_flushImmediately) { FileConnection.FileStream.Flush(); } } catch (Exception exception) { thrownException = exception; } } // ThreadAbortException should have a higher priority, and therefore we're doing rethrow in a separate block if (thrownException != null) throw thrownException; } }