/// <summary> /// Creates a LogFileWriter that initially queues message /// </summary> public LogFileWriterInMemory() { m_saveHelper = LogMessageSaveHelper.Create(); m_stream = new MemoryStream(); m_stream.Write(282497); //VersionNumber: Compressed. With LogSaveHelper m_zipStream = new DeflateStream(m_stream, CompressionMode.Compress, CompressionLevel.Level1, true); m_zipStream.FlushMode = FlushType.Sync; m_tmpBuffer = new byte[40960]; m_tmpStream = new MemoryStream(); m_logCount = 0; }
/// <summary> /// Creates a LogFileWriter that initially queues message /// </summary> public LogFileWriter(string logFileName) { CompressionLevel level = CompressionLevel.Level1; FileName = logFileName; m_saveHelper = LogMessageSaveHelper.Create(); FilePath.ValidatePathName(logFileName); m_stream = new FileStream(logFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read); m_stream.Write(282497); //VersionNumber: Compressed. With LogSaveHelper m_zipStream = new DeflateStream(m_stream, CompressionMode.Compress, level, true); m_zipStream.FlushMode = FlushType.Sync; m_tmpBuffer = new byte[40960]; m_tmpStream = new MemoryStream(); m_logCount = 0; m_syncRoot = new object(); }
/// <summary> /// Reads all log messages from the supplied file. /// </summary> public static List <LogMessage> Read(string logFileName) { List <LogMessage> lst = new List <LogMessage>(); FilePath.ValidatePathName(logFileName); using (var stream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { try { LogMessageSaveHelper helper; int version = stream.ReadInt32(); switch (version) { case 282497: helper = LogMessageSaveHelper.Create(); //VersionNumber: Compressed. With LogSaveHelper break; default: throw new VersionNotFoundException(); } using (var zipStream = new DeflateStream(stream, CompressionMode.Decompress, true)) using (var bs = new BufferedStream(zipStream)) { while (bs.ReadBoolean()) { var message = new LogMessage(bs, helper); lst.Add(message); } bs.Dispose(); } } catch (EndOfStreamException) { } catch (ZlibException) { } } return(lst); }
/// <summary> /// Writes the log data to the stream /// </summary> /// <param name="stream"></param> /// <param name="saveHelper"></param> internal void Save(Stream stream, LogMessageSaveHelper saveHelper = null) { if (saveHelper == null) { saveHelper = LogMessageSaveHelper.Create(true); } stream.Write((byte)4); saveHelper.SaveEventPublisherDetails(stream, EventPublisherDetails); saveHelper.SaveStackMessages(stream, InitialStackMessages); saveHelper.SaveStackTrace(stream, InitialStackTrace); saveHelper.SaveStackMessages(stream, CurrentStackMessages); saveHelper.SaveStackTrace(stream, CurrentStackTrace); stream.Write(UtcTime); LogMessageAttributes.Save(stream); saveHelper.SaveString(stream, Message); saveHelper.SaveString(stream, Details); saveHelper.SaveString(stream, ExceptionString); stream.Write(ManagedThreadID); stream.Write(PreviousFirstChanceExceptionSequenceNumber); }
/// <summary> /// Loads a log messages from the supplied stream /// </summary> /// <param name="stream">the stream to load the log message from.</param> /// <param name="saveHelper">A save helper that will condense objects</param> internal LogMessage(Stream stream, LogMessageSaveHelper saveHelper = null) { if (saveHelper == null) { saveHelper = LogMessageSaveHelper.Create(true); } byte version = stream.ReadNextByte(); switch (version) { case 1: EventPublisherDetails = saveHelper.LoadEventPublisherDetails(stream); InitialStackMessages = saveHelper.LoadStackMessages(stream); InitialStackTrace = saveHelper.LoadStackTrace(stream); CurrentStackMessages = saveHelper.LoadStackMessages(stream); CurrentStackTrace = saveHelper.LoadStackTrace(stream); UtcTime = stream.ReadDateTime(); LogMessageAttributes = new LogMessageAttributes(stream); Message = stream.ReadString(); Details = stream.ReadString(); Exception = null; ExceptionString = stream.ReadString(); ManagedThreadID = -1; PreviousFirstChanceExceptionSequenceNumber = -1; break; case 2: EventPublisherDetails = saveHelper.LoadEventPublisherDetails(stream); InitialStackMessages = saveHelper.LoadStackMessages(stream); InitialStackTrace = saveHelper.LoadStackTrace(stream); CurrentStackMessages = saveHelper.LoadStackMessages(stream); CurrentStackTrace = saveHelper.LoadStackTrace(stream); UtcTime = stream.ReadDateTime(); LogMessageAttributes = new LogMessageAttributes(stream); Message = stream.ReadString(); Details = stream.ReadString(); Exception = null; ExceptionString = stream.ReadString(); ManagedThreadID = stream.ReadInt32(); PreviousFirstChanceExceptionSequenceNumber = -1; break; case 3: EventPublisherDetails = saveHelper.LoadEventPublisherDetails(stream); InitialStackMessages = saveHelper.LoadStackMessages(stream); InitialStackTrace = saveHelper.LoadStackTrace(stream); CurrentStackMessages = saveHelper.LoadStackMessages(stream); CurrentStackTrace = saveHelper.LoadStackTrace(stream); UtcTime = stream.ReadDateTime(); LogMessageAttributes = new LogMessageAttributes(stream); Message = saveHelper.LoadString(stream); Details = saveHelper.LoadString(stream); Exception = null; ExceptionString = saveHelper.LoadString(stream); ManagedThreadID = stream.ReadInt32(); PreviousFirstChanceExceptionSequenceNumber = -1; break; case 4: EventPublisherDetails = saveHelper.LoadEventPublisherDetails(stream); InitialStackMessages = saveHelper.LoadStackMessages(stream); InitialStackTrace = saveHelper.LoadStackTrace(stream); CurrentStackMessages = saveHelper.LoadStackMessages(stream); CurrentStackTrace = saveHelper.LoadStackTrace(stream); UtcTime = stream.ReadDateTime(); LogMessageAttributes = new LogMessageAttributes(stream); Message = saveHelper.LoadString(stream); Details = saveHelper.LoadString(stream); Exception = null; ExceptionString = saveHelper.LoadString(stream); ManagedThreadID = stream.ReadInt32(); PreviousFirstChanceExceptionSequenceNumber = stream.ReadInt32(); break; default: throw new VersionNotFoundException(); } }