protected override void ConfigureLogging(IAppBuilder appBuilder, TextWriter logTarget, bool backgroundThreadLogging) { var logManagerConfig = appBuilder.GetLogManagerConfig(); var traceManagerConfig = appBuilder.GetTraceManagerConfig(); var traceSwitches = new SwitchSet() { { Tracer.All, new ThresholdTraceSwitch(TraceLevel.Verbose) } }; // Log to debugger var debuggerConfig = logManagerConfig.UseDebugger(); debuggerConfig.BackgroundLogging = backgroundThreadLogging; // Log to xunit without trace timestamps (default) var testOutputConfig = logManagerConfig.UseTestOutput(testOutputHelper); testOutputConfig.BackgroundLogging = backgroundThreadLogging; // Log to TextWriter with Trace timestamps var textLogConfig = logManagerConfig.UseTextWriter(logTarget) .Format(new DefaultTraceFormatter()); traceManagerConfig.TraceTo(new ILogWriterConfig[] { debuggerConfig, testOutputConfig, textLogConfig }, traceSwitches); appBuilder.LogHttpRequestsToAll(); appBuilder.UseOwinTracerLogging(); }
public static TraceWriterConfig TraceToTestOutput(this TraceManagerConfig traceManagerConfig, ITestOutputHelper testOutput, SwitchSet switchSet, EntryFormatter <TraceEntry> traceFormatter = null) { Contract.Requires <ArgumentNullException>(traceManagerConfig != null); Contract.Requires <ArgumentNullException>(testOutput != null); Contract.Requires <ArgumentNullException>(switchSet != null); return(TraceManagerConfigFluentExtensions.TraceTo(traceManagerConfig, new TestOutputLogWriterConfig(testOutput), switchSet, traceFormatter)); }
public void TestLogMatchesTextWriterLogs() { // Trace output is sent here var textWriter = new StringWriter(); // And also here var testOutput = new FakeTestOutputHelper(); var switches = new SwitchSet() { { Tracer.All, new OnOffTraceSwitch(true) } }; using (var traceManager = new TraceManager()) { var testOutputLogWriterConfig = traceManager.LogManager.Config.UseTestOutput(testOutput); testOutputLogWriterConfig.IncludeTimeOffset = false; // Turn this off so test log formatting matches the text logging default testOutputLogWriterConfig.IncludeTime = true; // Turn this on so test log formatting matches the text logging default traceManager.Config.TraceTo(testOutputLogWriterConfig, switches); traceManager.Config.TraceTo(textWriter, switches); // Some test traces var tracer = traceManager.TracerFor(this); tracer.Severe("Severe message"); tracer.Error("Error message"); tracer.Warn("Warning message"); TestHelper.WarnException(tracer, 5); tracer.Info("Info message"); tracer.Verbose("Verbose message"); tracer.Debug("Debug message"); Assert.True(traceManager.IsHealthy); } Assert.Equal(textWriter.ToString(), testOutput.GetTestOutput()); }
/// <summary> /// Creates a new <see cref="TraceManager" /> configured to use <paramref name="logWriter" /> and /// <paramref name="switches" /> for all <see cref="Tracer" />s. /// </summary> /// <param name="logWriter">The <see cref="IEntryWriter{TEntry}" /> to use.</param> /// <param name="switches">Defines the trace switches to use with <paramref name="logWriter" />.</param> public TraceManager(ILogWriter logWriter, SwitchSet switches) : this(new TraceWriterConfig(logWriter, switches)) { Contract.Requires <ArgumentNullException>(logWriter != null); Contract.Requires <ArgumentNullException>(switches != null); }