public LogItem(LogItemKind kind, string format, params object[] args) { Contract.Requires(kind.IsDefinedEnumValue(), nameof(kind)); Contract.Requires(!string.IsNullOrWhiteSpace(format), nameof(format)); Kind = kind; LoggedOn = DateTime.UtcNow; Message = string.Format(format, args); }
/// <summary> /// Check to see if the config specifies logging under the specific scenario. /// </summary> /// <param name="logInclusionKindToOriginsMap">Map of <see cref="LogItemKind" /> to a list of origins (null list is all, empty list is none).</param> /// <param name="logItemKind"><see cref="LogItemKind" /> to check.</param> /// <param name="logItemOrigin"><see cref="LogItemOrigin" /> to check.</param> /// <returns>A value indicating whether or not to log.</returns> protected static bool ShouldLog( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, LogItemKind logItemKind, string logItemOrigin) { new { logInclusionKindToOriginsMap }.AsArg().Must().NotBeNull(); var hasKey = logInclusionKindToOriginsMap.TryGetValue(logItemKind, out IReadOnlyCollection <string> origins); var result = !hasKey || (origins?.Contains(logItemOrigin) ?? true); return(result); }
/// <summary> /// Initializes a new instance of the <see cref="LogItem"/> class. /// </summary> /// <param name="subject">The core piece of information being logged.</param> /// <param name="kind">The kind of log-item.</param> /// <param name="context">The context within which the item was logged.</param> /// <param name="comment">Optional comment about the logged item.</param> /// <param name="correlations">Optional information about how this log-item is correlated with other/related log-items.</param> public LogItem( Subject subject, LogItemKind kind, LogItemContext context, string comment = null, IReadOnlyCollection <IHaveCorrelationId> correlations = null) { this.Subject = subject ?? throw new ArgumentNullException(nameof(subject)); if (kind == LogItemKind.Unknown) { throw new ArgumentException(Invariant($"{nameof(kind)} == {nameof(LogItemKind)}.{nameof(LogItemKind.Unknown)}")); } if ((correlations != null) && correlations.Any(_ => _ == null)) { throw new ArgumentException(Invariant($"{nameof(correlations)} contains a null element")); } this.Kind = kind; this.Context = context ?? throw new ArgumentNullException(nameof(context)); this.Comment = comment; this.Correlations = correlations ?? new IHaveCorrelationId[0]; }
/// <summary> /// Decide whether to include the item in <see cref="Console.Error" />. /// </summary> /// <param name="logItemKind">Kind of item.</param> /// <param name="logItemOrigin">Origin of item.</param> /// <returns>A value indicating whether to log.</returns> public bool ShouldLogError(LogItemKind logItemKind, string logItemOrigin) { return(ShouldLog(this.LogInclusionKindToOriginsMapForConsoleError, logItemKind, logItemOrigin)); }
/// <summary> /// Check to see if the config specifies logging under the specific scenario. /// </summary> /// <param name="logItemKind"><see cref="LogItemKind" /> to check.</param> /// <param name="logItemOrigin"><see cref="LogItemOrigin" /> to check.</param> /// <returns>A value indicating whether or not to log.</returns> public virtual bool ShouldLog(LogItemKind logItemKind, string logItemOrigin) { return(ShouldLog(this.LogInclusionKindToOriginsMap, logItemKind, logItemOrigin)); }