예제 #1
0
파일: Form1.cs 프로젝트: CharlieBP/NLog
        private void Form1_Load(object sender, EventArgs e)
        {

            RichTextBoxTarget target = new RichTextBoxTarget();
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            target.ControlName = "richTextBox1";
            target.FormName = "Form1";
            target.UseDefaultRowColoringRules = false;
            target.RowColoringRules.Add(
                    new RichTextBoxRowColoringRule(
                        "level >= LogLevel.Error and contains(message,'serious')", // condition
                        "White", // font color
                        "Red", // background color
                        FontStyle.Bold | FontStyle.Italic
                        )
                    );

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            Logger logger = LogManager.GetLogger("Example");
            logger.Trace("trace log message");
            logger.Debug("debug log message");
            logger.Info("info log message");
            logger.Warn("warn log message");
            logger.Error("error log message");
            logger.Fatal("fatal log message");
            logger.Fatal("fatal log message, rather serious");
        }
예제 #2
0
        public void ActiveFormNegativeTest2()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                FormName = "MyForm1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
            };

            using (Form form = new Form())
            {
                form.Name = "MyForm1";
                form.WindowState = FormWindowState.Minimized;
                form.Show();

                try
                {
                    target.Initialize(CommonCfg);
                    Assert.Fail("Expected exception.");
                }
                catch (NLogConfigurationException ex)
                {
                    Assert.IsNotNull(ex.InnerException);
                    Assert.AreEqual("Rich text box control name must be specified for RichTextBoxTarget.", ex.InnerException.Message);
                }
            }
        }
        public void SimpleRichTextBoxTargetTest()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
            logger.Fatal("Test");
            logger.Error("Foo");
            logger.Warn("Bar");
            logger.Info("Test");
            logger.Debug("Foo");
            logger.Trace("Bar");

            Application.DoEvents();

            var form = target.TargetForm;

            Assert.IsTrue(target.CreatedForm);
            Assert.IsTrue(form.Name.StartsWith("NLog"));
            Assert.AreEqual(FormWindowState.Normal, form.WindowState);
            Assert.AreEqual("NLog", form.Text);
            Assert.AreEqual(300, form.Width);
            Assert.AreEqual(200, form.Height);

            MemoryStream ms = new MemoryStream();
            target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
            string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

            Assert.IsTrue(target.CreatedForm);

            string expectedRtf = @"{\colortbl ;\red255\green255\blue255;\red255\green0\blue0;\red255\green165\blue0;\red0\green0\blue0;\red128\green128\blue128;\red169\green169\blue169;}
\viewkind4\uc1\pard\cf1\highlight2\b\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par
\cf2\highlight1\i Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par
\cf3\ul\b0\i0 Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
\cf4\ulnone Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par
\cf5 Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par
\cf6\i Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
\cf0\highlight0\i0\f1\par
}";
            Assert.IsTrue(rtfText.Contains(expectedRtf), "Invalid RTF: " + rtfText);

            LogManager.Configuration = null;
            Assert.IsNull(target.TargetForm);
            Application.DoEvents();
            Assert.IsTrue(form.IsDisposed);
        }
예제 #4
0
파일: Form1.cs 프로젝트: ExM/NLog
        private void Form1_Load(object sender, EventArgs e)
        {
            RichTextBoxTarget target = new RichTextBoxTarget();
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            target.ControlName = "richTextBox1";
            target.FormName = "Form1";
            target.UseDefaultRowColoringRules = true;

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            Logger logger = LogManager.GetLogger("Example");
            logger.Trace("trace log message");
            logger.Debug("debug log message");
            logger.Info("info log message");
            logger.Warn("warn log message");
            logger.Error("error log message");
            logger.Fatal("fatal log message");
        }
예제 #5
0
        private void InitializeLog()
        {
            try
            {
                logger = NLog.LogManager.GetCurrentClassLogger();
                NLog.Targets.RichTextBoxTarget target = new NLog.Targets.RichTextBoxTarget();

                target.Layout      = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
                target.FormName    = "DatabaseViewer";
                target.ControlName = "logText";
                target.UseDefaultRowColoringRules = true;

                NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #6
0
 static void Main(string[] args)
 {
     if (args == null || args.Length < 1)
     {
         RichTextBoxTarget target = new RichTextBoxTarget();
         target.Layout = "${message}";
         target.ControlName = "outputRTB";
         target.FormName = "PowerFlag";
         target.UseDefaultRowColoringRules = true;
         NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new PowerFlag());
     }
     else
     {
         if (args[0] == "-silent")
         {
             logger.Info("Started in silent mode.");
             PowerFlag.DoFlagging();
         }
     }
 }
예제 #7
0
        public void MaxLinesTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                    AutoScroll = true,
                };

                Assert.AreEqual(0, target.MaxLines);
                target.MaxLines = 7;

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                for (int i = 0; i < 100; ++i)
                {
                    logger.Info("Test {0}", i);
                }

                Application.DoEvents();
                string expectedText = "Test 93\nTest 94\nTest 95\nTest 96\nTest 97\nTest 98\nTest 99\n";

                Assert.AreEqual(expectedText, target.TargetRichTextBox.Text);
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
예제 #8
0
        public void ColoringRuleDefaults()
        {
            var expectedRules = new[]
            {
                new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "White", "Red", FontStyle.Bold),
                new RichTextBoxRowColoringRule("level == LogLevel.Error", "Red", "Empty", FontStyle.Bold | FontStyle.Italic),
                new RichTextBoxRowColoringRule("level == LogLevel.Warn", "Orange", "Empty", FontStyle.Underline),
                new RichTextBoxRowColoringRule("level == LogLevel.Info", "Black", "Empty"),
                new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Empty"),
                new RichTextBoxRowColoringRule("level == LogLevel.Trace", "DarkGray", "Empty", FontStyle.Italic),
            };

            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            ((ISupportsLazyParameters)target).CreateParameters(CommonCfg);

            var actualRules = target.DefaultRowColoringRules;
            Assert.AreEqual(expectedRules.Length, actualRules.Count);
            for (int i = 0; i < expectedRules.Length; ++i)
            {
                Assert.AreEqual(expectedRules[i].BackgroundColor, actualRules[i].BackgroundColor);
                Assert.AreEqual(expectedRules[i].FontColor, actualRules[i].FontColor);
                Assert.AreEqual(expectedRules[i].Condition.ToString(), actualRules[i].Condition.ToString());
                Assert.AreEqual(expectedRules[i].Style, actualRules[i].Style);
            }
        }
예제 #9
0
        public void AutoScrollTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${level} ${logger} ${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                    AutoScroll = true,
                };

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                for (int i = 0; i < 100; ++i)
                {
                    logger.Info("Test");
                    Application.DoEvents();
                    Assert.AreEqual(target.TargetRichTextBox.SelectionStart, target.TargetRichTextBox.TextLength);
                    Assert.AreEqual(target.TargetRichTextBox.SelectionLength, 0);
                }
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
예제 #10
0
        public void ActiveFormTest2()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                FormName = "MyForm2",
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            using (Form form = new Form())
            {
                form.Name = "MyForm1";
                form.WindowState = FormWindowState.Minimized;

                RichTextBox rtb = new RichTextBox();
                rtb.Dock = DockStyle.Fill;
                rtb.Name = "Control1";
                form.Controls.Add(rtb);
                form.Show();
                using (Form form1 = new Form())
                {
                    form1.Name = "MyForm2";
                    RichTextBox rtb2 = new RichTextBox();
                    rtb2.Dock = DockStyle.Fill;
                    rtb2.Name = "Control1";
                    form1.Controls.Add(rtb2);
                    form1.Show();
                    form1.Activate();

                    target.Initialize(CommonCfg);
                    Assert.AreSame(form1, target.TargetForm);
                    Assert.AreSame(rtb2, target.TargetRichTextBox);
                }
            }
        }
예제 #11
0
        public void CustomRowColoringTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${level} ${logger} ${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                    RowColoringRules =
                    {
                        new RichTextBoxRowColoringRule("starts-with(message, 'B')", "Maroon", "Empty"),
                    }
                };

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                logger.Fatal("Test");
                logger.Error("Foo");
                logger.Warn("Bar");
                logger.Info("Test");
                logger.Debug("Foo");
                logger.Trace("Bar");

                Application.DoEvents();

                var form = target.TargetForm;

                MemoryStream ms = new MemoryStream();
                target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
                string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

                Assert.True(target.CreatedForm);

                var result = rtfText;
                Assert.True(result.Contains(@"{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red128\green0\blue0;}"));
                Assert.True(result.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par"));
                Assert.True(result.Contains(@"Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par"));
                Assert.True(result.Contains(@"\cf3 Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par"));
                Assert.True(result.Contains(@"\cf1 Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par"));
                Assert.True(result.Contains(@"Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par"));
                Assert.True(result.Contains(@"\cf3 Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par"));
                Assert.True(result.Contains(@"\cf0\highlight0\f1\par"));
                Assert.True(result.Contains(@"}"));
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
예제 #12
0
        public void SimpleRichTextBoxTargetTest()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
            logger.Fatal(" {Test}rrr");
            logger.Error(" }F;o\\o");
            logger.Warn("Bar");
            logger.Info("Test");
            logger.Debug("Foo");
            logger.Trace("Bar");

            Application.DoEvents();

            var form = target.TargetForm;

            Assert.IsTrue(target.CreatedForm);
            Assert.IsTrue(form.Name.StartsWith("NLog"));
            Assert.AreEqual(FormWindowState.Normal, form.WindowState);
            Assert.AreEqual("NLog", form.Text);
            Assert.AreEqual(300, form.Width);
            Assert.AreEqual(200, form.Height);

            MemoryStream ms = new MemoryStream();
            target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
            string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

            Assert.IsTrue(target.CreatedForm);
            Console.WriteLine(rtfText);
            List<RtfParagraph> phs = RtfDocument.Load(rtfText);
            /*
            {\rtf1\ansi\ansicpg1251\deff0\deflang1049{\fonttbl{\f0\fnil\fcharset204 Microsoft Sans Serif;}}
            {\colortbl ;\red255\green255\blue255;\red255\green0\blue0;\red255\green165\blue0;\red0\green0\blue0;\red128\green128\blue128;\red169\green169\blue169;}
            \viewkind4\uc1\pard\cf1\highlight2\b\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests  \{Test\}rrr\par
            \cf2\highlight1\i Error NLog.UnitTests.Targets.RichTextBoxTargetTests  \}F;o\\o\par
            \cf3\ul\b0\i0 Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
            \cf4\ulnone Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par
            \cf5 Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par
            \cf6\i Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
            \cf0\highlight0\i0\par
            }
            */
            Color bc = target.TargetRichTextBox.BackColor;
            Color w = Color.FromName("White");
            Color r = Color.FromName("Red");
            Color o = Color.FromName("Orange");
            Color b = Color.FromName("Black");
            Color g = Color.FromName("Gray");
            Color dg = Color.FromName("DarkGray");

            CheckText(phs[0][0], "Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests  {Test}rrr", FontStyle.Bold, w, r);
            CheckText(phs[1][0], "Error NLog.UnitTests.Targets.RichTextBoxTargetTests  }F;o\\o", FontStyle.Bold | FontStyle.Italic, r, bc);
            CheckText(phs[2][0], "Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar", FontStyle.Underline, o, bc);
            CheckText(phs[3][0], "Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test", FontStyle.Regular, b, bc);
            CheckText(phs[4][0], "Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo", FontStyle.Regular, g, bc);
            CheckText(phs[5][0], "Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar", FontStyle.Italic, dg, bc);

            LogManager.Configuration = null;
            Assert.IsNull(target.TargetForm);
            Application.DoEvents();
            Assert.IsTrue(form.IsDisposed);
        }
        public void CustomWordRowColoringTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${level} ${logger} ${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                    WordColoringRules =
                    {
                        new RichTextBoxWordColoringRule("zzz", "Red", "Empty"),
                        new RichTextBoxWordColoringRule("aaa", "Green", "Empty"),
                    }
                };

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                logger.Fatal("Test zzz");
                logger.Error("Foo xxx");
                logger.Warn("Bar yyy");
                logger.Info("Test aaa");
                logger.Debug("Foo zzz");
                logger.Trace("Bar ccc");

                Application.DoEvents();

                var form = target.TargetForm;

                MemoryStream ms = new MemoryStream();
                target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
                string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

                Assert.IsTrue(target.CreatedForm);

                // "zzz" string will be highlighted

                string expectedRtf = @"{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red255\green0\blue0;\red0\green128\blue0;}
\viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par
Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo xxx\par
Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar yyy\par
Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf4\f1 aaa\cf1\f0\par
Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo \cf3\f1 zzz\cf1\f0\par
Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar ccc\par
\cf0\highlight0\f1\par
}";
                Assert.IsTrue(rtfText.Contains(expectedRtf), "Invalid RTF: " + rtfText);
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
        public void NoColoringTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${level} ${logger} ${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                };

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                logger.Fatal("Test");
                logger.Error("Foo");
                logger.Warn("Bar");
                logger.Info("Test");
                logger.Debug("Foo");
                logger.Trace("Bar");

                Application.DoEvents();

                var form = target.TargetForm;

                MemoryStream ms = new MemoryStream();
                target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
                string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

                Assert.IsTrue(target.CreatedForm);

                string expectedRtf = @"{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;}
\viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par
Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par
Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par
Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par
Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par
\cf0\highlight0\f1\par
}";
                Assert.IsTrue(rtfText.Contains(expectedRtf), "Invalid RTF: " + rtfText);
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
예제 #15
0
파일: MainForm.cs 프로젝트: fo-dicom/mdcm
        public void InitializeLog()
        {
            LoggingConfiguration config = new LoggingConfiguration();

            RichTextBoxTarget target = new RichTextBoxTarget();
            target.UseDefaultRowColoringRules = true;
            target.AutoScroll = true;
            target.Layout = "${message}";
            target.ControlName = rtbLog.Name;
            target.FormName = Name;

            config.AddTarget("DicomRichTextBox", target);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));

            LogManager.Configuration = config;
        }
예제 #16
0
        public void NoColoringTest()
        {
            try
            {
                RichTextBoxTarget target = new RichTextBoxTarget()
                {
                    ControlName = "Control1",
                    Layout = "${level} ${message}",
                    ShowMinimized = true,
                    ToolWindow = false,
                };

                SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
                logger.Fatal("Test");
                logger.Error("Foo");
                logger.Warn("Bar");
                logger.Info("Test");
                logger.Debug("Foo");
                logger.Trace("Bar");

                Application.DoEvents();

                MemoryStream ms = new MemoryStream();
                target.TargetRichTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
                string rtfText = Encoding.UTF8.GetString(ms.GetBuffer());

                Assert.IsTrue(target.CreatedForm);

                Console.WriteLine(rtfText);
                List<RtfParagraph> phs = RtfDocument.Load(rtfText);
            /*
            {\rtf1\ansi\ansicpg1251\deff0\deflang1049{\fonttbl{\f0\fnil\fcharset204 Microsoft Sans Serif;}}
            {\colortbl ;\red0\green0\blue0;\red255\green255\blue255;}
            \viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal Test\par
            Error Foo\par
            Warn Bar\par
            Info Test\par
            Debug Foo\par
            Trace Bar\par
            \cf0\highlight0\par
            }
            */
                Color fc = target.TargetRichTextBox.ForeColor;
                Color bc = target.TargetRichTextBox.BackColor;

                CheckText(phs[0][0], "Fatal Test", FontStyle.Regular, fc, bc);
                CheckText(phs[1][0], "Error Foo", FontStyle.Regular, fc, bc);
                CheckText(phs[2][0], "Warn Bar", FontStyle.Regular, fc, bc);
                CheckText(phs[3][0], "Info Test", FontStyle.Regular, fc, bc);
                CheckText(phs[4][0], "Debug Foo", FontStyle.Regular, fc, bc);
                CheckText(phs[5][0], "Trace Bar", FontStyle.Regular, fc, bc);
            }
            finally
            {
                LogManager.Configuration = null;
            }
        }
예제 #17
0
 public void RichTextBoxTargetDefaultsTest()
 {
     var target = new RichTextBoxTarget();
     Assert.IsFalse(target.UseDefaultRowColoringRules);
     Assert.AreEqual(0, target.WordColoringRules.Count);
     Assert.AreEqual(0, target.RowColoringRules.Count);
     Assert.IsNull(target.FormName);
     Assert.IsNull(target.ControlName);
 }
예제 #18
0
        internal static void InitFormLogger()
        {
            RichTextBoxTarget formControlTarget = new RichTextBoxTarget {
                AutoScroll = true,
                ControlName = "LogTextBox",
                FormName = "MainForm",
                Layout = GeneralLayout,
                MaxLines = byte.MaxValue,
                Name = "RichTextBox"
            };

            formControlTarget.RowColoringRules.Add(new RichTextBoxRowColoringRule("level >= LogLevel.Error", "Red", "Black"));
            formControlTarget.RowColoringRules.Add(new RichTextBoxRowColoringRule("level >= LogLevel.Warn", "Yellow", "Black"));

            LogManager.Configuration.AddTarget(formControlTarget);
            LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, formControlTarget));

            LogManager.ReconfigExistingLoggers();
        }
예제 #19
0
        public void ActiveFormTest()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                FormName = "MyForm1",
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            using (Form form = new Form())
            {
                form.Name = "MyForm1";
                form.WindowState = FormWindowState.Minimized;

                RichTextBox rtb = new RichTextBox();
                rtb.Dock = DockStyle.Fill;
                rtb.Name = "Control1";
                form.Controls.Add(rtb);
                form.Shown += (sender, e) =>
                    {
                        target.Initialize(CommonCfg);
                        form.Activate();
                        Application.DoEvents();
                        Assert.AreSame(form, target.TargetForm);
                        Assert.AreSame(rtb, target.TargetRichTextBox);
                        form.Close();
                    };

                form.ShowDialog();
                Application.DoEvents();
            }
        }
예제 #20
0
        private void InitLog()
        {
            LoggingConfiguration config = new LoggingConfiguration();
            RichTextBoxTarget control = new RichTextBoxTarget();
            control.Name = "RichTextBox";
            control.Layout = "${date:format=HH\\:mm\\:ss} |   ${message}";
            control.ControlName = "txtLog";
            control.FormName = "MainForm";
            control.AutoScroll = true;
            control.MaxLines = 10000;
            control.UseDefaultRowColoringRules = true;
            control.WordColoringRules.Add(new RichTextBoxWordColoringRule("True", "Green", "White", FontStyle.Bold));
            control.WordColoringRules.Add(new RichTextBoxWordColoringRule("False", "Red", "White", FontStyle.Bold));
            control.WordColoringRules.Add(new RichTextBoxWordColoringRule("SENT:", "Orange", "White"));
            control.WordColoringRules.Add(new RichTextBoxWordColoringRule("RECEIVED:", "Green", "White"));
            control.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "ControlText", "White"));
            control.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "White"));
            control.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "DarkRed", "Control"));
            control.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Error", "White", "DarkRed", FontStyle.Bold));
            control.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "Yellow", "DarkRed", FontStyle.Bold));
            config.AddTarget(control.Name, control);

            FileTarget fileTarget = new FileTarget();
            fileTarget.Name = "LogFile";
            fileTarget.Layout = "${date:format=HH\\:MM\\:ss} | ${level} | ${message}";
            fileTarget.CreateDirs = true;
            fileTarget.FileName = "${basedir}/logs/${date:format=yyyy-MM-dd}.log";
            fileTarget.ArchiveAboveSize = 5000000;
            fileTarget.ArchiveEvery = FileArchivePeriod.Day;
            fileTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling;
            fileTarget.MaxArchiveFiles = 10;
            fileTarget.ArchiveFileName = "${basedir}/logs/archive/${date:format=yyyy-MM-dd}.log";

            LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, control);
            config.LoggingRules.Add(rule1);
            LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule2);

            LogManager.Configuration = config;
        }
예제 #21
-1
        public void ActiveFormNegativeTest1()
        {
            RichTextBoxTarget target = new RichTextBoxTarget()
            {
                FormName = "MyForm1",
                ControlName = "Control1",
                UseDefaultRowColoringRules = true,
                Layout = "${level} ${logger} ${message}",
                ToolWindow = false,
                Width = 300,
                Height = 200,
            };

            using (Form form = new Form())
            {
                form.Name = "MyForm1";
                form.WindowState = FormWindowState.Minimized;

                //RichTextBox rtb = new RichTextBox();
                //rtb.Dock = DockStyle.Fill;
                //rtb.Name = "Control1";
                //form.Controls.Add(rtb);
                form.Show();
                try
                {
                    target.Initialize(CommonCfg);
                    Assert.Fail("Expected exception.");
                }
                catch (NLogConfigurationException ex)
                {
                    Assert.IsNotNull(ex.InnerException);
                    Assert.AreEqual("Rich text box control 'Control1' cannot be found on form 'MyForm1'.", ex.InnerException.Message);
                }
            }
        }