-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RichTextBox target always opens new form #133
Comments
I also see this as a problem. Whenever you want to use the RichTextBox target in the project, you only have two options:
private Target m_loggingTarget;
private LoggingRule m_loggingRule;
private void SetupLoggers()
{
LoggingConfiguration config = LogManager.Configuration;
RichTextBoxTarget textBoxTarget = new RichTextBoxTarget();
textBoxTarget.AutoScroll = true;
textBoxTarget.ControlName = logRichTextBox.Name;
textBoxTarget.FormName = this.Name;
textBoxTarget.MaxLines = 1024;
textBoxTarget.Name = "TextBox";
textBoxTarget.Layout = "${message} ${exception:format=tostring}";
textBoxTarget.RowColoringRules.Add(new RichTextBoxRowColoringRule("level==LogLevel.Warn", "Empty", "Empty", FontStyle.Bold));
textBoxTarget.RowColoringRules.Add(new RichTextBoxRowColoringRule("level==LogLevel.Error", "Red", "Empty", FontStyle.Bold));
textBoxTarget.RowColoringRules.Add(new RichTextBoxRowColoringRule("level==LogLevel.Trace", "Gray", "Empty", FontStyle.Regular));
m_loggingTarget = textBoxTarget;
config.AddTarget(m_loggingTarget.Name, m_loggingTarget);
m_loggingRule = new LoggingRule("*", LogLevel.Info, m_loggingTarget);
//add before final rules from config
config.LoggingRules.Insert(0, m_loggingRule);
LogManager.Configuration = config;
}
private void RemoveLoggers()
{
LoggingConfiguration config = LogManager.Configuration;
config.RemoveTarget(m_loggingTarget.Name);
config.LoggingRules.Remove(m_loggingRule);
LogManager.Configuration = config;
}
private void TestForm_Load(object sender, EventArgs e)
{
SetupLoggers();
}
private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
{
RemoveLoggers();
} I assume, that it is impractical from the performance view-point to check is textbox exists for every logging event. Still Maybe there are other way? One way could be to allow the targets to be declared in the file, but add methods to notify the configuration that components were created/destroyed. It could be extended even further - creating RichTextBox descendant doing registeration/unregisteraion by itself. What do you think? |
fixed with NLog/NLog.Windows.Forms#9 |
Reported in the forum at post: http://nlog-project.org/forum.html#nabble-tc1685688|a1685688
When I run the RichTextBox2 example, instead of logging to the Form1 richTextBox1 control, a new form is created and all the messages are logged to this new form. How do I get it to log to the control on Form1?
Comments:
dualcode wrote Apr 15 at 8:29 PM
I found the following to work
I found the following to NOT work
placing
private static Logger logger = LogManager.GetCurrentClassLogger();
at the top of the classsetting
logger = LogManager.GetCurrentClassLogger();
inside the constructor before or after InitializeComponent()ardabasoglu wrote Oct 19 2010 at 10:19 AM
If NLog cannot find the form named by "formName" attribute of RichTextBox target in configuration file, it opens a new window. In order to avoid that make sure you set the "formName" correctly and start logging after the form set as a target initializes. If logging starts before target form initializes, NLog could not find it even if you set it as a target correctly in configuration file.
The text was updated successfully, but these errors were encountered: