public void SaveStackMessages(Stream stream, LogStackMessages message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (ReferenceEquals(message, LogStackMessages.Empty)) { stream.Write((byte)0); } else if (m_isSimple) { stream.Write((byte)1); message.Save(stream); } else { int lookupId; if (m_stackMessagesSaveLookup.TryGetValue(message, out lookupId)) { stream.Write((byte)2); stream.Write(lookupId); } else { lookupId = m_stackMessagesSaveLookup.Count; stream.Write((byte)4); message.Save(stream); m_stackMessagesSaveLookup.Add(message, lookupId); } } }
/// <summary> /// Creates a log message /// </summary> internal LogMessage(LogEventPublisherDetails eventPublisherDetails, LogStackMessages initialStackMessages, LogStackTrace initialStackTrace, LogStackMessages currentStackMessages, LogStackTrace currentStackTrace, LogMessageAttributes flags, string message, string details, Exception exception) { if (eventPublisherDetails == null) { throw new ArgumentNullException(nameof(eventPublisherDetails)); } if (exception != null) { ExceptionString = exception.ToString(); } else { ExceptionString = string.Empty; } EventPublisherDetails = eventPublisherDetails; InitialStackMessages = initialStackMessages ?? LogStackMessages.Empty; InitialStackTrace = initialStackTrace ?? LogStackTrace.Empty; CurrentStackMessages = currentStackMessages ?? LogStackMessages.Empty; CurrentStackTrace = currentStackTrace ?? LogStackTrace.Empty; UtcTime = DateTime.UtcNow; LogMessageAttributes = flags; Message = message ?? string.Empty; Details = details ?? string.Empty; Exception = exception; ManagedThreadID = Thread.CurrentThread.ManagedThreadId; PreviousFirstChanceExceptionSequenceNumber = Logger.PreviousFirstChanceExceptionSequenceNumber; }
public void RemoveStackMessage(int depth) { while (m_threadStackDetails.Count >= depth) { m_threadStackDetails.RemoveAt(m_threadStackDetails.Count - 1); } m_stackMessageCache = null; }
/// <summary> /// Raises a log message with the provided data. /// </summary> /// <param name="overriddenAttributes">attributes to use with this message</param> /// <param name="message"></param> /// <param name="details">A long text field with the details of the message.</param> /// <param name="exception">An exception object if one is provided.</param> /// <param name="initialStackMessage"></param> /// <param name="initialStackTrace"></param> public void Publish(LogMessageAttributes?overriddenAttributes, string message, string details, Exception exception, LogStackMessages initialStackMessage, LogStackTrace initialStackTrace) { if (Logger.ShouldSuppressLogMessages) { return; } LogMessageAttributes attributes = overriddenAttributes ?? m_attributes; if (!m_publisher.HasSubscribers(attributes)) { return; } if (message == null) { message = string.Empty; } if (details == null) { details = string.Empty; } var suppressionFlags = m_supressionEngine.IncrementPublishCount(); if (suppressionFlags != MessageSuppression.None) { m_messagesSuppressed++; if (ShouldRaiseMessageSupressionNotifications && m_suppressionMessageNextPublishTime <= ShortTime.Now) { m_suppressionMessageNextPublishTime = ShortTime.Now.AddSeconds(10); MessageSuppressionIndication.Publish($"Message Suppression Is Occurring To: '{ m_owner.TypeData.TypeName }' {m_messagesSuppressed.ToString()} total messages have been suppressed.", m_owner.ToString()); } attributes += suppressionFlags; if (!m_publisher.HasSubscribers(attributes)) { return; } } LogStackMessages currentStackMessages = Logger.GetStackMessages(); LogStackTrace currentStackTrace = LogStackTrace.Empty; if (m_stackTraceDepth > 0) { currentStackTrace = new LogStackTrace(true, 2, m_stackTraceDepth); } else if (exception != null || attributes.Level >= MessageLevel.Error) { currentStackTrace = new LogStackTrace(true, 2, 10); } var logMessage = new LogMessage(m_owner, initialStackMessage, initialStackTrace, currentStackMessages, currentStackTrace, attributes, message, details, exception); m_logger.OnNewMessage(logMessage, m_publisher); }
internal LogPublisher(LoggerInternal logger, LogPublisherInternal publisherInstance, MessageClass classification) { m_logger = logger; m_publisherInstance = publisherInstance; m_classification = classification; InitialStackMessages = Logger.GetStackMessages(); InitialStackTrace = new LogStackTrace(true, 1, 10); MaxDistinctEventPublisherCount = 20; m_lookupEventPublishers = new ConcurrentDictionary <Tuple <LogMessageAttributes, string>, LogEventPublisherInternal>(); }
public StackDisposal AppendStackMessages(LogStackMessages messages) { m_stackMessageCache = null; m_threadStackDetails.Add(messages); int depth = m_threadStackDetails.Count; if (depth >= s_stackDisposalStackMessages.Length) { GrowStackDisposal(depth + 1); } return(s_stackDisposalStackMessages[depth]); }
public LogStackMessages LoadStackMessages(Stream stream) { byte version = stream.ReadNextByte(); switch (version) { case 0: return(LogStackMessages.Empty); case 1: return(new LogStackMessages(stream)); case 2: { if (m_isSimple) { throw new Exception("Cannot load without a LogMessageSaveHelper"); } int id = stream.ReadInt32(); return(m_stackMessagesLoadLookup[id]); } case 3: { if (m_isSimple) { throw new Exception("Cannot load without a LogMessageSaveHelper"); } int id = stream.ReadInt32(); var messages = new LogStackMessages(stream); m_stackMessagesLoadLookup.Add(messages); return(messages); } case 4: { if (m_isSimple) { throw new Exception("Cannot load without a LogMessageSaveHelper"); } var messages = new LogStackMessages(stream); m_stackMessagesLoadLookup.Add(messages); return(messages); } default: throw new VersionNotFoundException(); } }
public LogStackMessages GetStackMessages() { if (m_stackMessageCache == null) { if (m_threadStackDetails.Count == 0) { m_stackMessageCache = LogStackMessages.Empty; } else { m_stackMessageCache = new LogStackMessages(m_threadStackDetails); } } return(m_stackMessageCache); }
static Logger() { //Initializes the empty object of StackTraceDetails LogStackTrace.Initialize(); LogStackMessages.Initialize(); GrowStackDisposal(1); s_logger = new LoggerInternal(out s_logger); Console = new LogSubscriptionConsole(); FileWriter = new LogSubscriptionFileWriter(1000); AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Log = CreatePublisher(typeof(Logger), MessageClass.Component); Log.InitialStackTrace = LogStackTrace.Empty; EventFirstChanceException = Log.RegisterEvent(MessageLevel.NA, MessageFlags.SystemHealth, "First Chance App Domain Exception", 30, MessageRate.PerSecond(30), 1000); EventAppDomainException = Log.RegisterEvent(MessageLevel.Critical, MessageFlags.SystemHealth, "Unhandled App Domain Exception"); EventSwallowedException = Log.RegisterEvent(MessageLevel.Debug, MessageFlags.None, "Exception was Swallowed", 30, MessageRate.PerSecond(30), 1000); ShutdownHandler.Initialize(); }
/// <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(); } }
/// <summary> /// Temporarily appends data to the thread's stack so the data can be propagated to any messages generated on this thread. /// Be sure to call Dispose on the returned object to remove this from the stack. /// </summary> /// <param name="messages"></param> /// <returns></returns> public static IDisposable AppendStackMessages(LogStackMessages messages) { return(ThreadLocalThreadStack.Value.AppendStackMessages(messages)); }