コード例 #1
0
        /// <summary>
        /// Build a log item from provided values.
        /// </summary>
        /// <param name="correlationManager">Factory to produce correlations.</param>
        /// <param name="subjectFunc">Function that returns the subject.</param>
        /// <param name="comment">Optional comment.</param>
        /// <param name="originOverride">Optional origin override.</param>
        /// <param name="additionalCorrelations">Optional additional correlations.</param>
        /// <returns>Constructed <see cref="LogItem" />.</returns>
        public static LogItem BuildLogItem(
            IManageCorrelations correlationManager,
            Func <object> subjectFunc,
            string comment        = null,
            string originOverride = null,
            IReadOnlyCollection <IHaveCorrelationId> additionalCorrelations = null)
        {
            var timestampUtc = DateTime.UtcNow;
            var correlations = new List <IHaveCorrelationId>(additionalCorrelations ?? new IHaveCorrelationId[0]);

            var    subject    = subjectFunc?.Invoke();
            var    kind       = DetermineKindFromSubject(subject);
            string stackTrace = null;

            var managedCorrelations = correlationManager?.GetNextCorrelations() ?? new IHaveCorrelationId[0];

            correlations.AddRange(managedCorrelations);

            if (subject is Exception loggedException)
            {
                var exceptionCorrelations = correlationManager?.GetExceptionCorrelations(loggedException) ?? new IHaveCorrelationId[0];
                correlations.AddRange(exceptionCorrelations);

                stackTrace = loggedException.StackTrace;
            }

            var rawSubject = new RawSubject(
                subject,
                BuildSummaryFromSubjectObject(subject));

            var anonymousMethodInfo = subjectFunc != null ? new AnonymousMethodInfo(subjectFunc.Method) : null;
            var context             = new LogItemContext(
                timestampUtc,
                originOverride,
                MachineName,
                ProcessName,
                ProcessFileVersion,
                anonymousMethodInfo?.MethodName,
                anonymousMethodInfo?.EnclosingType?.ToRepresentation(),
                stackTrace);

            var result = new LogItem(rawSubject.ToSubject(), kind, context, comment, correlations);

            return(result);
        }
コード例 #2
0
ファイル: LogItem.cs プロジェクト: NaosProject/Naos.Logging
        /// <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];
        }