Hello all, I have a application that was originally a console App. Most of the stuff I have now changed to a more friendly UI using winforms. The only thing I am having problems with is getting log4net to append to a textbox.
my log4net config file.
and my appender class. in my mainform ServerGUI I call log.Warn("Testing warning");
but it does not append to my textbox.
Thanks
- xml version="1.0" encoding="utf-8" ?>
-
-
- <log4net>
-
- <appender name="textboxAppender" type="ServerGUI.TextBoxAppender, ServerGUI">
- <formName value="ServerGUI"/>
- <textBoxName value="txtConsole"/>
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
- layout>
- appender>
- <root>
- <level value="ALL" />
-
- <appender-ref ref="textboxAppender" />
- root>
-
- log4net>
- public class TextBoxAppender : AppenderSkeleton
- {
- private TextBox _textBox;
- public TextBox AppenderTextBox
- {
- get
- {
- return _textBox;
- }
- set
- {
- _textBox = value;
- }
- }
- public string FormName { get; set; }
- public string TextBoxName { get; set; }
-
- private Control FindControlRecursive(Control root, string textBoxName)
- {
- if (root.Name == textBoxName) return root;
- foreach (Control c in root.Controls)
- {
- Control t = FindControlRecursive(c, textBoxName);
- if (t != null) return t;
- }
- return null;
- }
-
- protected override void Append(log4net.Core.LoggingEvent loggingEvent)
- {
- if (_textBox == null)
- {
- if (String.IsNullOrEmpty(FormName) ||
- String.IsNullOrEmpty(TextBoxName))
- return;
-
- Form form = Application.OpenForms[FormName];
- if (form == null)
- return;
-
- _textBox = (TextBox)FindControlRecursive(form, TextBoxName);
- if (_textBox == null)
- return;
-
- form.FormClosing += (s, e) => _textBox = null;
- }
- _textBox.BeginInvoke((MethodInvoker)delegate
- {
- _textBox.AppendText(RenderLoggingEvent(loggingEvent));
- });
- }
- }