public void Log(LogEventInfo eventInfo) { dynamic otherEventInfo = ActivatorEx.CreateInstance(logEventInfoType); otherEventInfo.TimeStamp = eventInfo.Timestamp; otherEventInfo.Level = logLevelsMap[eventInfo.Level]; otherEventInfo.Message = eventInfo.Message; otherEventInfo.Exception = eventInfo.Exception; otherEventInfo.Properties["build-start"] = eventInfo.BuildStart; otherEventInfo.Properties["test-name"] = eventInfo.TestName; otherEventInfo.Properties["test-start"] = eventInfo.TestStart; logger.Log(otherEventInfo); }
private string BuildCompleteMessage(LogEventInfo eventInfo) { StringBuilder builder = new StringBuilder(); builder. Append(eventInfo.Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture)). Append(Separator). Append(eventInfo.Level.ToString(TermCase.Upper)). Append(Separator). Append(eventInfo.Message); if (eventInfo.Exception != null) builder.Append(Separator).Append(eventInfo.Exception.ToString()); return builder.ToString(); }
/// <summary> /// Ends the latest log section. /// </summary> public void EndSection() { if (sectionEndStack.Any()) { LogSection section = sectionEndStack.Pop(); TimeSpan duration = section.GetDuration(); LogEventInfo eventInfo = new LogEventInfo { Level = section.Level, Message = $"Finished: {section.Message} ({Math.Floor(duration.TotalSeconds)}.{duration:fff}s)", SectionEnd = section }; Log(eventInfo, true); } }
private void Log(LogEventInfo eventInfo, bool? withLogSectionEnd = null) { var appropriateConsumers = logConsumers. Where(x => eventInfo.Level >= x.MinLevel). Where(x => withLogSectionEnd == null || x.LogSectionFinish == withLogSectionEnd). Select(x => x.Consumer); foreach (ILogConsumer logConsumer in appropriateConsumers) logConsumer.Log(eventInfo); }
/// <summary> /// Starts the specified log section. /// </summary> /// <param name="section">The log section.</param> /// <example>This sample shows how to log the data insertion to some control in the scope of the control. /// <code> /// string value = "new_value"; /// Log.Start(new DataAdditionLogSection(this, value)); /// // TODO: Add a value to the control. /// Log.EndSection(); /// </code> /// </example> public void Start(LogSection section) { section.CheckNotNull(nameof(section)); LogEventInfo eventInfo = new LogEventInfo { Level = section.Level, Message = section.Message, SectionStart = section }; section.StartedAt = eventInfo.Timestamp; Log(eventInfo, false); eventInfo.Message = $"Starting: {eventInfo.Message}"; Log(eventInfo, true); sectionEndStack.Push(section); }
public void Log(LogEventInfo eventInfo) { string completeMessage = BuildCompleteMessage(eventInfo); Write(completeMessage); }
/// <inheritdoc/> protected override void OnLog(LogEventInfo eventInfo) { dynamic otherEventInfo = NLogAdapter.CreateLogEventInfo(eventInfo); Logger.Log(otherEventInfo); }