/// <summary> /// Initializes a new instance of the <see cref="TimeSlicedFilesLogConfig"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The log-item origins to log for.</param> /// <param name="logFileDirectoryPath">Directory path to write log files to.</param> /// <param name="fileNamePrefix">File name to use for each log file as a prefix and a unique time slice hash will be used as the suffix.</param> /// <param name="timeSlicePerFile">Amount of time to store in each file.</param> /// <param name="createDirectoryStructureIfMissing">Optional value indicating whether to create the directory structure if it's missing; DEFAULT is true.</param> /// <param name="logItemPropertiesToIncludeInLogMessage"> The properties/aspects of a <see cref="LogItem"/> to include when building a log message.</param> public TimeSlicedFilesLogConfig( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, string logFileDirectoryPath, string fileNamePrefix, TimeSpan timeSlicePerFile, bool createDirectoryStructureIfMissing = true, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage = LogItemPropertiesToIncludeInLogMessage.Default) : base(logInclusionKindToOriginsMap, logItemPropertiesToIncludeInLogMessage) { if (string.IsNullOrWhiteSpace(logFileDirectoryPath)) { throw new ArgumentException(Invariant($"{nameof(logFileDirectoryPath)} is null or white space")); } if (string.IsNullOrWhiteSpace(fileNamePrefix)) { throw new ArgumentException(Invariant($"{nameof(fileNamePrefix)} is null or white space")); } if (timeSlicePerFile == default(TimeSpan)) { throw new ArgumentException(Invariant($"{nameof(timeSlicePerFile)} is equal to the default {nameof(TimeSpan)}")); } this.LogFileDirectoryPath = logFileDirectoryPath; this.FileNamePrefix = fileNamePrefix; this.CreateDirectoryStructureIfMissing = createDirectoryStructureIfMissing; this.TimeSlicePerFile = timeSlicePerFile; this.sliceOffsets = timeSlicePerFile.SliceIntoOffsetsPerDay(); }
/// <summary> /// Initializes a new instance of the <see cref="ConsoleLogConfig"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The items to log to this logger.</param> /// <param name="logInclusionKindToOriginsMapForConsoleOut">The items to log to <see cref="Console.Out" />.</param> /// <param name="logInclusionKindToOriginsMapForConsoleError">The items to log to <see cref="Console.Error" />.</param> /// <param name="logItemPropertiesToIncludeInLogMessage"> The properties/aspects of a <see cref="LogItem"/> to include when building a log message.</param> public ConsoleLogConfig( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMapForConsoleOut, IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMapForConsoleError, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage = LogItemPropertiesToIncludeInLogMessage.Default) : base(logInclusionKindToOriginsMap, logItemPropertiesToIncludeInLogMessage) { this.LogInclusionKindToOriginsMapForConsoleOut = logInclusionKindToOriginsMapForConsoleOut; this.LogInclusionKindToOriginsMapForConsoleError = logInclusionKindToOriginsMapForConsoleError; this.LogInclusionKindToOriginsMapForConsoleOutFriendlyString = GenerateFriendlyStringFromLogInclusionKindToOriginsMap(logInclusionKindToOriginsMapForConsoleOut); this.LogInclusionKindToOriginsMapForConsoleErrorFriendlyString = GenerateFriendlyStringFromLogInclusionKindToOriginsMap(logInclusionKindToOriginsMapForConsoleError); }
/// <summary> /// Initializes a new instance of the <see cref="InMemoryLogConfig"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The log-item origins to log for.</param> /// <param name="maxLoggedItemCount">Optional maximum number of elements to keep internally before removing the oldest items; DEFAULT is -1 which is infinite.</param> /// <param name="logItemPropertiesToIncludeInLogMessage"> The properties/aspects of a <see cref="LogItem"/> to include when building a log message.</param> public InMemoryLogConfig( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, int maxLoggedItemCount = -1, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage = LogItemPropertiesToIncludeInLogMessage.Default) : base(logInclusionKindToOriginsMap, logItemPropertiesToIncludeInLogMessage) { if (maxLoggedItemCount < -1) { throw new ArgumentOutOfRangeException(Invariant($"{nameof(maxLoggedItemCount)} is <= -1; value is {maxLoggedItemCount}")); } this.MaxLoggedItemCount = maxLoggedItemCount; }
/// <summary> /// Initializes a new instance of the <see cref="EventLogConfig"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The log-item origins to log for.</param> /// <param name="source">Optional event log source; DEFAULT is running process' name.</param> /// <param name="shouldCreateSourceIfMissing">Value indicating whether or not to create the source if missing.</param> /// <param name="logName">Optional log name; DEFAULT is <see cref="DefaultLogName" />.</param> /// <param name="machineName">Optional machine name; DEFAULT is <see cref="DefaultMachineName" />.</param> /// <param name="logItemPropertiesToIncludeInLogMessage"> The properties/aspects of a <see cref="LogItem"/> to include when building a log message.</param> public EventLogConfig( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, string source = null, string logName = DefaultLogName, string machineName = DefaultMachineName, bool shouldCreateSourceIfMissing = false, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage = LogItemPropertiesToIncludeInLogMessage.Default) : base(logInclusionKindToOriginsMap, logItemPropertiesToIncludeInLogMessage) { this.Source = string.IsNullOrWhiteSpace(source) ? ProcessHelpers.GetRunningProcess().GetName() : source; this.ShouldCreateSourceIfMissing = shouldCreateSourceIfMissing; this.LogName = string.IsNullOrWhiteSpace(logName) ? DefaultLogName : logName; this.MachineName = string.IsNullOrWhiteSpace(machineName) ? DefaultMachineName : machineName; }
/// <summary> /// Initializes a new instance of the <see cref="FileLogConfig"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The log-item origins to log for.</param> /// <param name="logFilePath">File path to write logs to.</param> /// <param name="createDirectoryStructureIfMissing">Optional value indicating whether to create the directory structure if it's missing; DEFAULT is true.</param> /// <param name="logItemPropertiesToIncludeInLogMessage"> The properties/aspects of a <see cref="LogItem"/> to include when building a log message.</param> public FileLogConfig( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, string logFilePath, bool createDirectoryStructureIfMissing = true, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage = LogItemPropertiesToIncludeInLogMessage.Default) : base(logInclusionKindToOriginsMap, logItemPropertiesToIncludeInLogMessage) { if (string.IsNullOrWhiteSpace(logFilePath)) { throw new ArgumentException(Invariant($"{nameof(logFilePath)} is null or white space")); } this.LogFilePath = logFilePath; this.CreateDirectoryStructureIfMissing = createDirectoryStructureIfMissing; }
/// <summary> /// Initializes a new instance of the <see cref="LogWriterConfigBase"/> class. /// </summary> /// <param name="logInclusionKindToOriginsMap">The log items to log.</param> /// <param name="logItemPropertiesToIncludeInLogMessage">The properties/aspects of an <see cref="LogItem"/> to include when building a log message.</param> /// <param name="logItemSerializerRepresentation">Optional serializer representation; DEFAULT is JSON.</param> protected LogWriterConfigBase( IReadOnlyDictionary <LogItemKind, IReadOnlyCollection <string> > logInclusionKindToOriginsMap, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage, SerializerRepresentation logItemSerializerRepresentation = null) { new { logInclusionKindToOriginsMap }.AsArg().Must().NotBeNull(); this.LogInclusionKindToOriginsMap = logInclusionKindToOriginsMap; this.LogItemPropertiesToIncludeInLogMessage = logItemPropertiesToIncludeInLogMessage; this.LogInclusionKindToOriginsMapFriendlyString = GenerateFriendlyStringFromLogInclusionKindToOriginsMap(this.LogInclusionKindToOriginsMap); this.LogItemSerializerRepresentation = logItemSerializerRepresentation ?? new SerializerRepresentation( SerializationKind.Json, typeof(LoggingJsonSerializationConfiguration).ToRepresentation()); }
public static string BuildLogMessageFromLogItem( LogItem logItem, LogItemPropertiesToIncludeInLogMessage logItemPropertiesToIncludeInLogMessage, ISerializer serializer, bool appendTrailingNewLine = false) { if (logItem == null) { throw new ArgumentNullException(nameof(logItem)); } if (logItemPropertiesToIncludeInLogMessage == LogItemPropertiesToIncludeInLogMessage.None) { return(string.Empty); } var stringBuilder = new StringBuilder(); var itemsToLog = logItemPropertiesToIncludeInLogMessage.GetIndividualFlags <LogItemPropertiesToIncludeInLogMessage>().Where(_ => _.GetIndividualFlags().Count == 1).OrderBy(_ => (int)_).ToList(); if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.LogItemSerialization)) { var serializedLogItem = serializer.SerializeToString(logItem); stringBuilder.AppendLine(); stringBuilder.Append(serializedLogItem); stringBuilder.Append(","); } else { const string indentationPadding = " "; const string firstLineDelimiter = "|"; if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.Timestamp)) { stringBuilder.Append(logItem.Context.TimestampUtc.ToString("u", CultureInfo.InvariantCulture)); stringBuilder.Append(firstLineDelimiter); } if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.SubjectSummary)) { stringBuilder.Append("Summary: "); stringBuilder.Append(logItem.Subject.Summary); stringBuilder.Append(firstLineDelimiter); } if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.Comment)) { stringBuilder.Append("Comment: "); stringBuilder.Append(logItem.Comment ?? LogHelper.NullSubjectSummary); stringBuilder.Append(firstLineDelimiter); } // close out first line stringBuilder.AppendLine(); if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.Origin)) { stringBuilder.Append(indentationPadding); stringBuilder.Append("Origin: "); stringBuilder.AppendLine(logItem.Context.Origin); } if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.Kind)) { stringBuilder.Append(indentationPadding); stringBuilder.Append("Kind: "); stringBuilder.AppendLine(logItem.Kind.ToString()); } if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.CorrelationTypeAndIds)) { stringBuilder.Append(indentationPadding); stringBuilder.AppendLine("Correlations:"); foreach (var correlation in logItem.Correlations) { stringBuilder.Append(indentationPadding); stringBuilder.Append(indentationPadding); stringBuilder.AppendLine(correlation.ToString()); } } if (itemsToLog.Contains(LogItemPropertiesToIncludeInLogMessage.StackTrace)) { stringBuilder.Append(indentationPadding); stringBuilder.AppendLine("StackTrace:"); stringBuilder.Append(indentationPadding); stringBuilder.Append(indentationPadding); stringBuilder.Append(logItem.Context.StackTrace ?? LogHelper.NullSubjectSummary); stringBuilder.AppendLine(); } } if (appendTrailingNewLine) { stringBuilder.AppendLine(); } return(stringBuilder.ToString()); }