Skip to content

2016

Richtextbox Output Log4net Appender

Use RichTextBox as an output of Log4net Appender

Step 1


  • Create a Windows Forms Application project named "Sample" using Visual Studio (the code is in c#).

Step 2


  • Get log4net library by either downloading it from https://logging.apache.org/log4net/ or by installing the package through NuGet Package Manager.

Step 3


  • Create folder "lib" under the project and place files there.
  • Add reference to lib\log4net.dll

Step 4


  • Please rename default form to "FrmSample". On the default form add a button and a RichTextBox.
  • Please name the RichTextBox as "RtbOutput" and a button as "CmdGenerateTestLogs".

Step 5


  • In the root of the project folder create XML file: log4net.config
  • In Properties for the file set: Copy to Output Directory: Copy if newer
  • Populate file with following (log file appender will create a file in logs folder with the file name of output.log; once file size reaches 10MB, new file will be created. Maximum of 10 log files will be retained). Please change "Sample" with the name of your project namespace, form name "FrmSample" with the name of the windows form name, text box name "RtbOutput" with the name of the RichTextBox that would be used to output log4net messages:
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="file" value="logs\output.log" />
    <param name="appendToFile" value="true" />
    <param name="rollingStyle" value="Size" />
    <param name="maxSizeRollBackups" value="10" />
    <param name="maximumFileSize" value="10MB" />
    <param name="staticLogFileName" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="conversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="RichTextBoxAppender" type="Sample.RichTextBoxAppender, Sample">
    <formName value="FrmSample"/>
    <textBoxName value="RtbOutput"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </root>
</log4net>

Step 6


  • To enable logging, under the Properties folder double-click on AssemblyInfo.cs and add the following line at the end of the file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Step 7


  • Add a new class RichTextBoxAppender.cs to the project. Paste the following into the class file replacing existing text:
using System.Drawing;
using System.Windows.Forms;
using log4net.Appender;
using log4net.Core;

namespace Sample
{
    public class RichTextBoxAppender : AppenderSkeleton
    {
        private RichTextBox _textBox;
        public RichTextBox AppenderTextBox { get { return _textBox; } set { _textBox = value; } }
        public string FormName { get; set; }
        public string TextBoxName { get; set; }

        private static Control FindControlRecursive(Control root, string textBoxName)
        {
            if (root.Name == textBoxName) return root;
            foreach (Control c in root.Controls)
            {
                var t = FindControlRecursive(c, textBoxName);
                if (t != null) return t;
            }
            return null;
        }

        protected override void Append(LoggingEvent loggingEvent)
        {
            if (_textBox == null)
            {
                if (string.IsNullOrEmpty(FormName) || string.IsNullOrEmpty(TextBoxName)) return;

                var form = Application.OpenForms[FormName];
                if (form == null) return;

                _textBox = (RichTextBox)FindControlRecursive(form, TextBoxName);
                if (_textBox == null) return;

                form.FormClosing += (s, e) => _textBox = null;
            }
            _textBox.BeginInvoke((MethodInvoker)delegate
            {
                if (loggingEvent.Level == Level.Debug)
                {
                    _textBox.SelectionStart = _textBox.TextLength;
                    _textBox.SelectionLength = 0;
                    _textBox.SelectionColor = Color.RoyalBlue;
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                    _textBox.SelectionColor = _textBox.ForeColor;
                }
                else if (loggingEvent.Level == Level.Info)
                {
                    _textBox.SelectionStart = _textBox.TextLength;
                    _textBox.SelectionLength = 0;
                    _textBox.SelectionColor = Color.ForestGreen;
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                    _textBox.SelectionColor = _textBox.ForeColor;
                }
                else if (loggingEvent.Level == Level.Warn)
                {
                    _textBox.SelectionStart = _textBox.TextLength;
                    _textBox.SelectionLength = 0;
                    _textBox.SelectionColor = Color.DarkOrange;
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                    _textBox.SelectionColor = _textBox.ForeColor;
                }
                else if (loggingEvent.Level == Level.Error)
                {
                    _textBox.SelectionStart = _textBox.TextLength;
                    _textBox.SelectionLength = 0;
                    _textBox.SelectionColor = Color.DarkRed;
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                    _textBox.SelectionColor = _textBox.ForeColor;
                }
                else if (loggingEvent.Level == Level.Fatal)
                {
                    _textBox.SelectionStart = _textBox.TextLength;
                    _textBox.SelectionLength = 0;
                    _textBox.SelectionColor = Color.Crimson;
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                    _textBox.SelectionColor = _textBox.ForeColor;
                }
                else
                {
                    _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                }
            });
        }
    }
}

Step 8


  • Open the code behind the form by pressing F7 on the form and paste following code:
using System;
using System.Drawing;
using System.Windows.Forms;
using log4net;

namespace Sample
{
    public partial class FrmSample : Form
    {

        private static readonly ILog Log = LogManager.GetLogger(typeof(FrmSample));

        public FrmSample()
        {
            InitializeComponent();

            #region Set properties for RtbOutput as per your choice
            RtbOutput.BackColor = System.Drawing.SystemColors.Control;
            RtbOutput.HideSelection = false;
            RtbOutput.ReadOnly = true;
            RtbOutput.Text = "";
            RtbOutput.WordWrap = false;
            RtbOutput.Font = new Font(FontFamily.GenericMonospace, 9);
            #endregion
        }

        private void CmdGenerateTestLogs_Click(object sender, EventArgs e)
        {
            Log.Debug("This is a DEBUG message");
            Log.Info("This is a INFO message");
            Log.Warn("This is a WARN message");
            Log.Error("This is a ERROR message");
            Log.Fatal("This is a FATAL message");
        }
    }
}

Step 9


  • Running program and clicking on the button should print out the log (same log will appear in the output.log file under log folder - check the project output directory). Feel free to modify the properties of the RichTextBox as you fit.

Open Ports Verizon Router Fios G1100

Forward port on Verizon FiOS Router G1100

Information Required:

  • Local IP Address of FiOS Router (by default 192.168.1.1) : {IP_ADDRESS_OF_FIOS_ROUTER}
  • Username and Password to login to the FiOS Router (usually on the sticker on your router).
  • Local IP Address of the Device that should be servicing Forwarded Port.
  • Port to be forwarded.

Steps:

  • Open browser and type in the http://IP_ADDRESS_OF_FIOS_ROUTER.
  • Type in "User Name" and "Password" of your router.
  • Choose "Firewall" from the top menu.
  • Click "Yes" button on the "Warning / Any changes made in this section may affect your router's performance and configuration. / Do you want to proceed?" screen.
  • Click "Port Forwarding" link on the left menu.
  • Under "Create new port forwarding rule" in the first drop down box "Select IP from menu" to select an IP address of the device on your network that should be accepting the request from the remote computer.
  • Click on "Application To Forward..." dropdown and choose "Custom Ports".
  • Choose the protocol: TCP, UDP, or Both and overwrite port 65535 with the port number of your choice.
  • Click "Add".
  • Do NOT CLICK "Advanced>>" and choose port that way.
  • Click "Logout" link on the left menu.
  • The "Port Forwarding" is completed.