public void LogToActiveLogWriters( string logItemOrigin, LogEntry logEntry) { logEntry = logEntry ?? throw new ArgumentNullException(nameof(logEntry)); LogItem logItem; try { logItem = this.BuildLogItem(logItemOrigin, logEntry); } catch (Exception ex) { string serializedLogEntry; try { serializedLogEntry = this.failingSerializerFactory.BuildSerializer(this.failingSerializerRepresentation).SerializeToString(logEntry); } catch (Exception failToSerializeLogEntryException) { serializedLogEntry = Invariant($"Failed to serialize log entry: {logEntry.Subject} with error: {failToSerializeLogEntryException.Message}"); } var updatedSubject = new InvalidLogEntryException(serializedLogEntry, ex); var newLogEntry = new LogEntry(updatedSubject); try { logItem = this.BuildLogItem(LogItemOrigin.NaosLoggingLogWriting.ToString(), newLogEntry); } catch (Exception failedToBuildInvalidLogEntryException) { var rawSubject = new RawSubject( new FailedToBuildInvalidLogEntryException( "Failed to build invalid log entry.", failedToBuildInvalidLogEntryException), serializedLogEntry); logItem = new LogItem(rawSubject.ToSubject(), LogItemKind.Exception, new LogItemContext(DateTime.UtcNow, LogItemOrigin.NaosLoggingLogWriting.ToString())); } } this.LogToActiveLogWriters(logItem); }
public LogItem BuildLogItem( string logItemOrigin, LogEntry logEntry, IReadOnlyCollection <IHaveCorrelationId> additionalCorrelations = null) { logEntry = logEntry ?? throw new ArgumentNullException(nameof(logEntry)); var psuedoOrderCorrelationPosition = GetSomewhatFakePositionFromLogEntry(logEntry); if (!string.IsNullOrWhiteSpace(logEntry.Category)) { throw new ArgumentException(Invariant($"{nameof(LogEntry)} cannot have the property {nameof(LogEntry.Category)} set; found: {logEntry.Category}.")); } if (psuedoOrderCorrelationPosition != 1 && (logEntry.Params?.Any() ?? false)) { throw new ArgumentException(Invariant($"{nameof(LogEntry)} cannot have the property {nameof(LogEntry.Params)} set unless it's part of the {nameof(LogActivity.Trace)} scenario; found: {logEntry.Params.Select(_ => _.ToString()).ToCsv()}")); } object logItemSubjectObject; var correlations = new List <IHaveCorrelationId>(additionalCorrelations ?? new IHaveCorrelationId[0]); var correlationId = Guid.NewGuid().ToString().ToLowerInvariant(); if (psuedoOrderCorrelationPosition == -1) { logItemSubjectObject = logEntry.Subject; } else { if ((psuedoOrderCorrelationPosition == 0) || (psuedoOrderCorrelationPosition == 2)) { logItemSubjectObject = logEntry.Subject; } else if (psuedoOrderCorrelationPosition == 1) { if (logEntry.Params?.Any() ?? false) { if (logEntry.Params.Count() > 1) { throw new InvalidOperationException(Invariant($"{nameof(LogEntry)} cannot have the property {nameof(LogEntry.Params)} set with more than one value as part of the {nameof(LogActivity.Trace)} scenario; found: {logEntry.Params.Select(_ => _.ToString()).ToCsv()}")); } else { logItemSubjectObject = logEntry.Params.Single(); } } else { logItemSubjectObject = logEntry.Message; } } else { throw new NotSupportedException(Invariant($"This fake order correlation position is not supported: {psuedoOrderCorrelationPosition}")); } var activityCorrelatingSubject = new RawSubject( logEntry.Subject, LogHelper.BuildSummaryFromSubjectObject(logEntry.Subject)); correlationId = activityCorrelatingSubject.GetHashCode().ToGuid().ToString().ToLowerInvariant(); var elapsedMilliseconds = psuedoOrderCorrelationPosition == 0 ? 0 : logEntry.ElapsedMilliseconds ?? throw new InvalidOperationException(Invariant($"{nameof(logEntry)}.{nameof(LogEntry.ElapsedMilliseconds)} is null when there is an {nameof(ElapsedCorrelation)}")); var elapsedCorrelation = new ElapsedCorrelation(correlationId, TimeSpan.FromMilliseconds(elapsedMilliseconds)); var correlatingSubject = activityCorrelatingSubject.ToSubject(); correlations.Add(elapsedCorrelation); var subjectCorrelation = new SubjectCorrelation(correlationId, correlatingSubject); correlations.Add(subjectCorrelation); var orderCorrelation = new OrderCorrelation(correlationId, psuedoOrderCorrelationPosition); correlations.Add(orderCorrelation); } string stackTrace = null; var kind = LogHelper.DetermineKindFromSubject(logItemSubjectObject); if (logItemSubjectObject is Exception loggedException) { var exceptionCorrelations = Logging.Domain.CorrelationManager.BuildExceptionCorrelations(loggedException, correlationId, this.errorCodeKeysField); correlations.AddRange(exceptionCorrelations); stackTrace = loggedException.StackTrace; } switch (psuedoOrderCorrelationPosition) { case 0: logItemSubjectObject = UsingBlockLogger.InitialItemOfUsingBlockSubject; break; case 2: logItemSubjectObject = UsingBlockLogger.FinalItemOfUsingBlockSubject; break; } var logItemRawSubject = new RawSubject( logItemSubjectObject, LogHelper.BuildSummaryFromSubjectObject(logItemSubjectObject)); var context = new LogItemContext(logEntry.TimeStamp, logItemOrigin, LogHelper.MachineName, LogHelper.ProcessName, LogHelper.ProcessFileVersion, logEntry.CallingMethod, logEntry.CallingType?.ToRepresentation(), stackTrace); var comment = (logItemSubjectObject is string logItemSubjectAsString) && (logItemSubjectAsString == logEntry.Message) ? null : logEntry.Message; var result = new LogItem(logItemRawSubject.ToSubject(), kind, context, comment, correlations); return(result); }