/// <summary> /// Creates and returns a new <see cref="TraceManagerConfig" /> with default trace configuration, /// which means if a debugger is attached, trace output is logged to the debugger. /// </summary> /// <returns></returns> public static TraceManagerConfig Default(LogManagerConfig logManagerConfig) { var traceManagerConfig = new TraceManagerConfig(logManagerConfig); traceManagerConfig.SetToDefaultConfiguration(); return(traceManagerConfig); }
/// <summary> /// Creates a new <see cref="LogManager" /> instance using the specified <paramref name="logManagerConfig" /> for /// configuration, and an optional <paramref name="setupTracerFactory" /> for logging LogJam setup and internal operations. /// </summary> /// <param name="logManagerConfig"> /// The <see cref="LogManagerConfig" /> that describes how this <see cref="LogManager" /> /// should be setup. /// </param> /// <param name="setupTracerFactory">The <see cref="ITracerFactory" /> to use for tracking internal operations.</param> public LogManager(LogManagerConfig logManagerConfig, ITracerFactory setupTracerFactory = null) { Contract.Requires <ArgumentNullException>(logManagerConfig != null); _setupTracerFactory = setupTracerFactory ?? new SetupLog(); _config = logManagerConfig; _backgroundMultiLogWriters = new List <BackgroundMultiLogWriter>(); _logWriters = new Dictionary <ILogWriterConfig, ILogWriter>(); }
public static TestOutputLogWriterConfig UseTestOutput(this LogManagerConfig logManagerConfig, ITestOutputHelper testOutput) { Contract.Requires <ArgumentNullException>(logManagerConfig != null); Contract.Requires <ArgumentNullException>(testOutput != null); var testOutputLogWriterConfig = new TestOutputLogWriterConfig(testOutput); logManagerConfig.Writers.Add(testOutputLogWriterConfig); return(testOutputLogWriterConfig); }
public void BackgroundMultiLogWriterToText() { // Just to ensure that formats + writes occur on a background thread int testThreadId = Thread.CurrentThread.ManagedThreadId; // Log output written here on the background thread var stringWriter = new StringWriter(); EntryFormatAction <LoggingTimer.StartRecord> formatStart = (startRecord, writer) => { Assert.NotEqual(testThreadId, Thread.CurrentThread.ManagedThreadId); writer.BeginEntry(); writer.WriteField((buffer) => buffer.AppendFormat(">{0}", startRecord.TimingId)); writer.EndEntry(); }; EntryFormatAction <LoggingTimer.StopRecord> formatStop = (stopRecord, writer) => { Assert.NotEqual(testThreadId, Thread.CurrentThread.ManagedThreadId); writer.BeginEntry(); writer.WriteField((buffer) => buffer.AppendFormat("<{0}", stopRecord.TimingId)); writer.WriteField(stopRecord.ElapsedTime.ToString()); writer.EndEntry(); }; var logManagerConfig = new LogManagerConfig(); logManagerConfig.UseTextWriter(stringWriter) .Format(formatStart) .Format(formatStop) .BackgroundLogging = true; using (var logManager = new LogManager(logManagerConfig)) { // LoggingTimer test class logs starts and stops LoggingTimer.RestartTimingIds(); var timer = new LoggingTimer("test LoggingTimer", logManager); var timing1 = timer.Start(); Thread.Sleep(15); timing1.Stop(); var timing2 = timer.Start(); Thread.Sleep(10); timing2.Stop(); } string logOutput = stringWriter.ToString(); _testOutputHelper.WriteLine(logOutput); Assert.Contains(">2\r\n<2 00:00:00.", logOutput); Assert.Contains(">3\r\n<3 00:00:00.", logOutput); }
/// <summary> /// Empty trace configuration, connected to <paramref name="logManagerConfig"/>. /// </summary> public TraceManagerConfig(LogManagerConfig logManagerConfig) { Contract.Requires <ArgumentNullException>(logManagerConfig != null); LogManagerConfig = logManagerConfig; _traceWriterConfigs = new ObservableSet <TraceWriterConfig>(); _traceWriterConfigs.AddingItem += OnAddingTraceWriterConfig; _traceWriterConfigs.RemovingItem += OnRemovingTraceWriterConfig; var observableLogWriterConfigSet = (ObservableSet <ILogWriterConfig>)logManagerConfig.Writers; observableLogWriterConfigSet.RemovingItem += OnRemovingLogWriterConfig; }
public void LogManagerCanBePassedToTraceManagerCtor() { var setupLog = new SetupLog(); var logConfig = new LogManagerConfig(); // Just a logwriter that is not used for tracing var messageListWriter = new ListLogWriter <MessageEntry>(setupLog); var messageListWriterConfig = logConfig.UseLogWriter(messageListWriter); var logManager = new LogManager(logConfig, setupLog); // The TraceManagerConfig includes a DebugTraceWriter, which adds to the LogManager.Writers using (var traceManager = new TraceManager(logManager, TraceManagerConfig.CreateDebugTraceWriterConfig())) { traceManager.Start(); // Starting the TraceManager starts the LogManager Assert.True(logManager.IsStarted); // There should be two started LogWriters - one is the DebuggerLogWriter for tracing; the other is messageListWriter Assert.Equal(2, logManager.Config.Writers.Where(writerConfig => ((IStartable)logManager.GetLogWriter(writerConfig)).IsStarted).Count()); Assert.True(logManager.IsHealthy); // Ensure no warnings or errors } }
/// <summary> /// Enables the default text formatters for entry types that are logged when <see cref="ILogger"/> instances are used for logging. /// </summary> /// <param name="logManagerConfig"></param> internal static void AddDefaultLoggerTextFormatters(LogManagerConfig logManagerConfig) { logManagerConfig.Writers.FormatAll <LoggerEntry>(new DefaultLoggerEntryFormatter()); logManagerConfig.Writers.FormatAll(new GenericLoggerBeginScopeEntryFormatter()); logManagerConfig.Writers.FormatAll(new GenericLoggerEndScopeEntryFormatter()); }