/// <summary>
        /// Sets formatting for entry types <typeparamref name="TEntry" /> using <paramref name="formatAction" />.
        /// </summary>
        /// <typeparam name="TEntry"></typeparam>
        /// <param name="formatAction"></param>
        /// <returns></returns>
        public TextLogWriterConfig Format <TEntry>(EntryFormatAction <TEntry> formatAction)
            where TEntry : ILogEntry
        {
            Contract.Requires <ArgumentNullException>(formatAction != null);

            return(Format((EntryFormatter <TEntry>)formatAction));
        }
        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);
        }
Exemple #3
0
        public void MultiLogWriterToText()
        {
            // Log output written here
            var stringWriter = new StringWriter();

            var setupTracerFactory = new SetupLog();
            EntryFormatAction <LoggingTimer.StartRecord> formatStart = (startRecord, writer) =>
            {
                writer.BeginEntry();
                writer.WriteField((buffer) => buffer.AppendFormat(">{0}", startRecord.TimingId));
                writer.EndEntry();
            };
            EntryFormatAction <LoggingTimer.StopRecord> formatStop = (stopRecord, writer) =>
            {
                writer.BeginEntry();
                writer.WriteField((buffer) => buffer.AppendFormat("<{0}", stopRecord.TimingId));
                writer.WriteField(stopRecord.ElapsedTime.ToString());
                writer.EndEntry();
            };
            var logWriter = new TextLogWriter(setupTracerFactory, new TextWriterFormatWriter(setupTracerFactory, stringWriter))
                            .AddFormat(formatStart)
                            .AddFormat(formatStop);

            using (var logManager = new LogManager(logWriter))
            {
                // 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);
        }
Exemple #4
0
        public void CustomTraceFormatting()
        {
            // Text output is written here
            StringWriter traceOutput = new StringWriter();

            // Can either use a EntryFormatAction, or subclass EntryFormatter<TEntry>.  Here we're using a EntryFormatAction.
            // Note that subclassing EntryFormatter<TEntry> provides a slightly more efficient code-path.
            EntryFormatAction <TraceEntry> format = (traceEntry, formatWriter) =>
            {
                formatWriter.BeginEntry();
                formatWriter.WriteField(traceEntry.TraceLevel.ToString());
                formatWriter.EndEntry();
            };

            using (var traceManager = new TraceManager(new TextWriterLogWriterConfig(traceOutput).Format(format)))
            {
                var tracer = traceManager.TracerFor(this);
                tracer.Info("m");
                tracer.Error("m");
            }

            Assert.Equal("Info\r\nError\r\n", traceOutput.ToString());
        }
        public EntryActionFormatter(EntryFormatAction <TEntry> formatAction)
        {
            Contract.Requires <ArgumentNullException>(formatAction != null);

            _formatAction = formatAction;
        }