示例#1
0
        public void FormattedListenerWithNameAndFileNameWillUseFormatterIfExists()
        {
            string fileName = Path.GetTempFileName();

            try
            {
                using (FormattedTextWriterTraceListener listener
                           = new FormattedTextWriterTraceListener(fileName, "name", new TextFormatter("DUMMY{newline}DUMMY")))
                {
                    TraceEventCache eventCache = new TraceEventCache();
                    listener.TraceData(
                        eventCache,
                        "cat1",
                        TraceEventType.Error,
                        0,
                        new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
                }

                Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", File.ReadAllText(fileName));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
示例#2
0
        /// <summary>
        /// 添加文本框控件的Listener
        /// </summary>
        /// <param name="textBox"></param>
        public void AddTextBoxTraceListener(TextBox textBox)
        {
            ExceptionHelper.FalseThrow(this.logger != null, "Logger对象为空");

            FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(new TextBoxWriter(textBox));

            listener.Formatter = new TextLogFormatter("textLogFormatter"); //LoggingSection.GetConfig().LogFormatterElements["TextLogFormatter"]

            this.logger.Listeners.Add(listener);
        }
        private static LogWriter CreateStandardOutputLogWriter()
        {
            var traceListener = new FormattedTextWriterTraceListener(
                Console.OpenStandardOutput(), new TextFormatter("{timestamp}:\t{message}\n"));
            var config = new LoggingConfiguration();

            config.AddLogSource("StandardOutput", System.Diagnostics.SourceLevels.All, true)
            .AddAsynchronousTraceListener(traceListener);
            return(new LogWriter(config));
        }
示例#4
0
 public void FormattedListenerWillUseFormatterIfExists()
 {
     using (StringWriter writer = new StringWriter())
     {
         FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));
         LogSource source = new LogSource("notfromconfig", SourceLevels.All);
         source.Listeners.Add(listener);
         source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
         Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", writer.ToString());
     }
 }
        public void FormattedListenerWillUseFormatterIfExists()
        {
            StringWriter writer = new StringWriter();
            FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);
            source.Listeners.Add(listener);
            source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));

            Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", writer.ToString());
        }
示例#6
0
 public void ShouldLogApplyingLog()
 {
     using (StringWriter writer = new StringWriter())
     {
         FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));
         listener.Filter = new EventTypeFilter(SourceLevels.Error);
         LogSource source = new LogSource("notfromconfig", SourceLevels.All);
         source.Listeners.Add(listener);
         source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Critical, "title", null));
         Assert.AreNotEqual(0, writer.ToString().Length);
     }
 }
示例#7
0
        public void FormattedListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
        {
            LogEntry testEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);

            using (StringWriter writer = new StringWriter())
            {
                FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer);
                LogSource source = new LogSource("notfromconfig", SourceLevels.All);
                source.Listeners.Add(listener);
                source.TraceData(TraceEventType.Error, 0, testEntry);
                string writtenData       = writer.ToString();
                string testEntryToString = testEntry.ToString();
                Assert.IsTrue(-1 != writtenData.IndexOf(testEntryToString));
            }
        }
        public void FormattedListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
        {
            LogEntry testEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
            StringWriter writer = new StringWriter();
            FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer);

            // need to go through the source to get a TraceEventCache
            LogSource source = new LogSource("notfromconfig", SourceLevels.All);
            source.Listeners.Add(listener);
            source.TraceData(TraceEventType.Error, 0, testEntry);

            string writtenData = writer.ToString();
            string testEntryToString = testEntry.ToString();

            Assert.IsTrue(-1 != writtenData.IndexOf(testEntryToString));
        }
        public void ShouldLogApplyingLog()
        {
            using (StringWriter writer = new StringWriter())
            {
                FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));

                listener.Filter = new EventTypeFilter(SourceLevels.Error);

                LogSource source = new LogSource("notfromconfig", SourceLevels.All);
                source.Listeners.Add(listener);

                source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Critical, "title", null));

                Assert.AreNotEqual(0, writer.ToString().Length);
            }
        }
        public void MessageIsFormattedWhenNoFormattingUsingJsonFormatterWithFormattedTextWriterTraceListener()
        {
            using (StringWriter writer = new StringWriter())
            {
                FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new JsonLogFormatter(JsonFormatting.None));

                LoggingConfiguration loggingConfiguration = BuildProgrammaticConfigForTrace();
                loggingConfiguration.AddLogSource("cat1", SourceLevels.All, true, listener);

                this.writer = new LogWriter(loggingConfiguration);
                var logEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
                this.writer.Write(logEntry);

                Assert.IsTrue(writer.ToString().IndexOf("\"Message\":\"message\",\"Categories\":[\"cat1\"],\"Priority\":0,\"EventId\":0,\"Severity\":2,\"LoggedSeverity\":\"Error\",\"Title\":\"title\",\"TimeStamp\":") != -1);
            }
        }
        public void FormattedListenerWithNameAndFileNameWillUseFormatterIfExists()
        {
            string fileName = Path.GetTempFileName();

            try
            {
                using (FormattedTextWriterTraceListener listener
                    = new FormattedTextWriterTraceListener(fileName, "name", new TextFormatter("DUMMY{newline}DUMMY")))
                {
                    TraceEventCache eventCache = new TraceEventCache();
                    listener.TraceData(
                        eventCache,
                        "cat1",
                        TraceEventType.Error,
                        0,
                        new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
                }

                Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", File.ReadAllText(fileName));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
示例#12
0
        /// <summary>
        /// 添加文本框控件的Listener
        /// </summary>
        /// <param name="textBox"></param>
        public void AddTextBoxTraceListener(TextBox textBox)
        {
            ExceptionHelper.FalseThrow(this.logger != null, "Logger对象为空");

            FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(new TextBoxWriter(textBox));

            listener.Formatter = new TextLogFormatter("textLogFormatter"); //LoggingSection.GetConfig().LogFormatterElements["TextLogFormatter"]

            this.logger.Listeners.Add(listener);
        }