/// <summary> /// Checks whether log event should be logged or not. /// </summary> /// <param name="logEvent">Log event.</param> /// <returns> /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/> /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/> /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/> /// </returns> protected internal override FilterResult Check(LogEventInfo logEvent) { if (_condition == null) return FilterResult.Neutral; object val = _condition.Evaluate(logEvent); if (val != null && val is bool && ((bool)val)) return Result; else return FilterResult.Neutral; }
/// <summary> /// Renders the specified environmental information and appends it to the specified <see cref="StringBuilder" />. /// </summary> /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param> /// <param name="logEvent">Logging event.</param> protected internal abstract void Append(StringBuilder builder, LogEventInfo logEvent);
/// <summary> /// Returns the estimated number of characters that are needed to /// hold the rendered value for the specified logging event. /// </summary> /// <param name="logEvent">Logging event information.</param> /// <returns>The number of characters.</returns> /// <remarks> /// If the exact number is not known or /// expensive to calculate this function should return a rough estimate /// that's big enough in most cases, but not too big, in order to conserve memory. /// </remarks> protected internal abstract int GetEstimatedBufferSize(LogEventInfo logEvent);
/// <summary> /// Adds the log event to asynchronous queue to be processed by /// the lazy writer thread. /// </summary> /// <param name="logEvent">The log event.</param> /// <remarks> /// The <see cref="Target.PrecalculateVolatileLayouts"/> is called /// to ensure that the log event can be processed in another thread. /// </remarks> protected internal override void Write(LogEventInfo logEvent) { WrappedTarget.PrecalculateVolatileLayouts(logEvent); RequestQueue.Enqueue(logEvent); }
/// <summary> /// Checks whether log event should be logged or not. /// </summary> /// <param name="logEvent">Log event.</param> /// <returns> /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/> /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/> /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/> /// </returns> protected internal override FilterResult Check(LogEventInfo logEvent) { if (IgnoreCase) { if (CompiledLayout.GetFormattedMessage(logEvent).ToLower().IndexOf(Substring.ToLower()) < 0) return Result; else return FilterResult.Neutral; } else { if (CompiledLayout.GetFormattedMessage(logEvent).IndexOf(Substring) < 0) return Result; else return FilterResult.Neutral; } }
/// <summary> /// Checks whether log event should be logged or not. /// </summary> /// <param name="logEvent">Log event.</param> /// <returns> /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/> /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/> /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/> /// </returns> protected internal abstract FilterResult Check(LogEventInfo logEvent);
/// <summary> /// Checks whether log event should be logged or not. /// </summary> /// <param name="logEvent">Log event.</param> /// <returns> /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/> /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/> /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/> /// </returns> protected internal override FilterResult Check(LogEventInfo logEvent) { if (0 == String.Compare(CompiledLayout.GetFormattedMessage(logEvent), CompareTo, IgnoreCase)) return Result; else return FilterResult.Neutral; }
/// <summary> /// Writes the specified array of logging events to a file specified in the FileName /// parameter. /// </summary> /// <param name="logEvents">An array of <see cref="LogEventInfo "/> objects.</param> /// <remarks> /// This function makes use of the fact that the events are batched by sorting /// the requests by filename. This optimizes the number of open/close calls /// and can help improve performance. /// </remarks> protected internal override void Write(LogEventInfo[] logEvents) { if (_fileNameLayout.IsAppDomainFixed) { foreach (LogEventInfo lei in logEvents) Write(lei); return; } Array.Sort(logEvents, 0, logEvents.Length, _logEventComparer); lock (this) { string currentFileName = null; MemoryStream ms = new MemoryStream(); LogEventInfo firstLogEvent = null; for (int i = 0; i < logEvents.Length; ++i) { LogEventInfo logEvent = logEvents[i]; string logEventFileName = _fileNameLayout.GetFormattedMessage(logEvent); if (logEventFileName != currentFileName) { if (currentFileName != null) { if (ShouldAutoArchive(currentFileName, firstLogEvent, (int)ms.Length)) { WriteFooterAndUninitialize(currentFileName); InvalidateCacheItem(currentFileName); DoAutoArchive(currentFileName, firstLogEvent); } WriteToFile(currentFileName, ms.ToArray(), false); } currentFileName = logEventFileName; firstLogEvent = logEvent; ms.SetLength(0); ms.Position = 0; } byte[] bytes = GetBytesToWrite(logEvent); ms.Write(bytes, 0, bytes.Length); } if (currentFileName != null) { if (ShouldAutoArchive(currentFileName, firstLogEvent, (int)ms.Length)) { WriteFooterAndUninitialize(currentFileName); InvalidateCacheItem(currentFileName); DoAutoArchive(currentFileName, firstLogEvent); } WriteToFile(currentFileName, ms.ToArray(), false); } } }
/// <summary> /// Writes the specified logging event to a file specified in the FileName /// parameter. /// </summary> /// <param name="logEvent">The logging event.</param> protected internal override void Write(LogEventInfo logEvent) { lock (this) { string fileName = _fileNameLayout.GetFormattedMessage(logEvent); byte[] bytes = GetBytesToWrite(logEvent); if (ShouldAutoArchive(fileName, logEvent, bytes.Length)) { InvalidateCacheItem(fileName); DoAutoArchive(fileName, logEvent); } WriteToFile(fileName, bytes, false); } }
/// <summary> /// Gets the bytes to be written to the file. /// </summary> /// <param name="logEvent">log event</param> /// <returns>array of bytes that are ready to be written</returns> protected virtual byte[] GetBytesToWrite(LogEventInfo logEvent) { string renderedText = GetFormattedMessage(logEvent) + NewLineChars; return TransformBytes(_encoding.GetBytes(renderedText)); }
/// <summary> /// Formats the log event for write. /// </summary> /// <param name="logEvent">The log event to be formatted.</param> /// <returns>A string representation of the log event.</returns> protected virtual string GetFormattedMessage(LogEventInfo logEvent) { return CompiledLayout.GetFormattedMessage(logEvent); }
private bool ShouldAutoArchive(string fileName, LogEventInfo ev, int upcomingWriteSize) { if (_archiveAboveSize == -1 && _archiveEvery == ArchiveEveryMode.None) return false; DateTime lastWriteTime; long fileLength; if (!GetFileInfo(fileName, out lastWriteTime, out fileLength)) return false; if (_archiveAboveSize != -1) { if (fileLength + upcomingWriteSize > _archiveAboveSize) return true; } if (_archiveEvery != ArchiveEveryMode.None) { string formatString; switch (_archiveEvery) { case ArchiveEveryMode.Year: formatString = "yyyy"; break; case ArchiveEveryMode.Month: formatString = "yyyyMM"; break; default: case ArchiveEveryMode.Day: formatString = "yyyyMMdd"; break; case ArchiveEveryMode.Hour: formatString = "yyyyMMddHH"; break; case ArchiveEveryMode.Minute: formatString = "yyyyMMddHHmm"; break; } string ts = lastWriteTime.ToString(formatString); string ts2 = ev.TimeStamp.ToString(formatString); if (ts != ts2) return true; } return false; }
private void DoAutoArchive(string fileName, LogEventInfo ev) { FileInfo fi = new FileInfo(fileName); if (!fi.Exists) return; // Console.WriteLine("DoAutoArchive({0})", fileName); string fileNamePattern; if (_autoArchiveFileName == null) { string ext = Path.GetExtension(fileName); fileNamePattern = Path.ChangeExtension(fi.FullName, ".{#}" + ext); } else { fileNamePattern = _autoArchiveFileName.GetFormattedMessage(ev); } switch (ArchiveNumbering) { case ArchiveNumberingMode.Rolling: RecursiveRollingRename(fi.FullName, fileNamePattern, 0); break; case ArchiveNumberingMode.Sequence: SequentialArchive(fi.FullName, fileNamePattern); break; } }
/// <summary> /// Evaluates the specified text by expadinging all layout renderers. /// </summary> /// <param name="text">The text to be evaluated.</param> /// <param name="logEvent">Log event to be used for evaluation</param> /// <returns>The input text with all occurences of ${} replaced with /// values provided by the appropriate layout renderers.</returns> public static string Evaluate(string text, LogEventInfo logEvent) { Layout l = new Layout(text); return l.GetFormattedMessage(logEvent); }
/// <summary> /// Precalculates the layout for the specified log event and stores the result /// in per-log event cache. /// </summary> /// <param name="logEvent">The log event.</param> /// <remarks> /// Calling this method enables you to store the log event in a buffer /// and/or potentially evaluate it in another thread even though the /// layout may contain thread-dependent renderer. /// </remarks> public void Precalculate(LogEventInfo logEvent) { //Console.WriteLine("Precalculating {0}", this.Text); GetFormattedMessage(logEvent); }
/// <summary> /// Renders the layout for the specified logging event by invoking layout renderers /// that make up the event. /// </summary> /// <param name="logEvent">The logging event.</param> /// <returns>The rendered layout.</returns> public string GetFormattedMessage(LogEventInfo logEvent) { if (_fixedText != null) return _fixedText; string cachedValue = logEvent.GetCachedLayoutValue(this); if (cachedValue != null) return cachedValue; int size = 0; for (int i = 0; i < _renderers.Length; ++i) { LayoutRenderer app = _renderers[i]; try { int ebs = app.GetEstimatedBufferSize(logEvent); size += ebs; } catch (Exception ex) { if (InternalLogger.IsWarnEnabled) { InternalLogger.Warn("Exception in {0}.GetEstimatedBufferSize(): {1}.", app.GetType().FullName, ex); } } } StringBuilder builder = new StringBuilder(size); for (int i = 0; i < _renderers.Length; ++i) { LayoutRenderer app = _renderers[i]; try { app.Append(builder, logEvent); } catch (Exception ex) { if (InternalLogger.IsWarnEnabled) { InternalLogger.Warn("Exception in {0}.Append(): {1}.", app.GetType().FullName, ex); } } } string value = builder.ToString(); logEvent.AddCachedLayoutValue(this, value); return value; }