Skip to content
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

Closed
ghost opened this issue Oct 7, 2012 · 2 comments
Closed

RichTextBox target always opens new form #133

ghost opened this issue Oct 7, 2012 · 2 comments
Labels
bug Bug report / Bug fix

Comments

@ghost
Copy link

ghost commented Oct 7, 2012

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

public partial class Form1 : Form
{
private static Logger logger;// = LogManager.GetCurrentClassLogger();

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
logger = LogManager.GetCurrentClassLogger();
logger.Info("Now we can log as Application.OpenForms[\"Form\1"] exists");
}
}

I found the following to NOT work
placing private static Logger logger = LogManager.GetCurrentClassLogger(); at the top of the class

setting 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.

@mikhail-barg
Copy link

I also see this as a problem. Whenever you want to use the RichTextBox target in the project, you only have two options:

  1. Specify the target in the config file, but call any logging-related code only after all affected Form.Load events fires, as proposed above. This way you can't log program start o any other code that happens before form load. And you can't do readonly logger fields. Also, this is not working in case forms are created sometime later than the actual application starts.
  2. Dynamically modify LoggingConfiguration on Form.Load and FormClosed events adding and removing corresponding targets and rules. This is what we do for our applications, but this is really cumbersome:
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?

@304NotModified
Copy link
Member

fixed with NLog/NLog.Windows.Forms#9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report / Bug fix
Projects
None yet
Development

No branches or pull requests

2 participants