TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Poppa Wallace
NA
189
24.4k
log4net custom appender
Dec 19 2016 8:44 AM
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));
});
}
}
Reply
Answers (
1
)
data type error
Detect Beacon using estimote SDK