Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring log4net TextBoxAppender (custom appender) via Xml file

This is in followup to my question: Flexible Logging Interface...

I now want to write a custom log4net appender for a multiline TextBox, for my WinForms 2.0 application. One of the StackOverflow members devdigital has already pointed me to this link:

TextBox Appender

However, the article does not describe how to configure such an appender via an Xml file. The unique problem in configuring this appender is that we need to pass a reference to a TextBox object to this appender.

So is it at all possible to configure it using an Xml file? Or can such appenders be only configured programmatically? What are the options to make it as configurable or loosely coupled as possible, may be using a combination of Xml file and code?

Thanks.

like image 217
AllSolutions Avatar asked Jan 01 '13 21:01

AllSolutions


People also ask

What is rolling file Appender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

Where do I put log4net in web config?

Tell log4net to Load Your Config cs file. You can find it under the Properties section in your project: Add this to the bottom of your AssemblyInfo file.


1 Answers

It depends on the way how you configure log4net, but usually there will be no forms created(and thus textBoxes) when log4net reads configuration. So, you need to create properties for form and textbox names. And you should check if form is opened and it has provided textbox just before appending logging event. Also it's better to inherit from AppenderSkeleton than implement IAppender from scratch:

public class TextBoxAppender : AppenderSkeleton
{
    private TextBox _textBox;
    public string FormName { get; set; }
    public string TextBoxName { get; set; }

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

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

            _textBox = form.Controls[TextBoxName] as TextBox;
            if (_textBox == null)
                return;

            form.FormClosing += (s, e) => _textBox = null;
        }

        _textBox.AppendText(loggingEvent.RenderedMessage + Environment.NewLine);
    }
}

Configuration is simple (log4net will read xml elements and provide values for properties with same names):

<appender name="textbox" type="Foo.TextBoxAppender, Foo">
  <formName value="Form1"/>
  <textBoxName value="textBox1"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger - %message" />
  </layout>      
</appender>
<root>
  <level value="INFO" />
  <appender-ref ref="textbox"/>
</root>

I didn't provide any error handling code or code related to multi-threading and threads synchronization, because question is about appender configuration.

like image 97
Sergey Berezovskiy Avatar answered Oct 02 '22 14:10

Sergey Berezovskiy