Alvin
Alvin

Reputation: 8499

NLog control to an existing RichTextBox Windows Form

Below is my NLog configuration, I want to load log into existing RichTextBox called rtMessage in Form1, but NLog will create a new windows with log message loaded into the RichTextBox:

 <targets>
    <target xsi:type="RichTextBox" name="m" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" 
            controlName="rtMessage" formName="Form1" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="m" />
  </rules>

Thank you.

Upvotes: 6

Views: 13036

Answers (5)

BigChief
BigChief

Reputation: 1515

Don't forget to add

LogManager.ReconfigExistingLoggers();

if setting the richtextbox as a logger programmatically

Upvotes: 1

Jason
Jason

Reputation: 307

https://github.com/NLog/NLog.Windows.Forms/wiki/RichTextBoxTarget

I have the similar problems to nlog into the windows.form. I used the NLog.Windows.Forms from the Nugget with the Nlog, and Nlog.config. I follow that given templatefrom the link and make it work without any manual initialisation. I provide part of the code into the Nlog.config. Hope it helps.

<targets>
<target xsi:type="RichTextBox"
      name="richTextBox1"
      layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
      height="30"
      autoScroll="true"
      maxLines="60"
      showMinimized="true"
      toolWindow="true"
      controlName="richTextBox1"
      formName="Form1"
      width="50"
      useDefaultRowColoringRules="true"
      allowAccessoryFormCreation="true"
      messageRetention="None"
      supportLinks="false"
>
</target>
</targets>

Upvotes: 0

MoonAche
MoonAche

Reputation: 23

Here are three tips to help you load log in existing RichTextBox.

  1. Ensure formName and controlName is consistent with the actual use
  2. Set allowAccessoryFormCreation="False" in NLog.config
  3. RichTextBoxTarget.ReInitializeAllTextboxes(this); in Form_Load

PS:Follow this way, you neeed to config your RichTextBoxTarget in NLog.config, please refer RictTextBoxTarget.

Upvotes: 1

crifan
crifan

Reputation: 14328

1.normally init logger in winform1_Load while it have done InitializeComponent -> has init your own RichTextBox.

2.then makesure your RichTextBoxTarget's FormName and ControlName initialized ok. such as:

RichTextBoxTarget rtbTarget = new RichTextBoxTarget();
logConfig.AddTarget("richTextBox", rtbTarget);
rtbTarget.FormName = "frmScrapeAmazonProduct"; // your winform class name
rtbTarget.ControlName = "rtbLog"; // your RichTextBox control/variable name

more can refer my post

Upvotes: 2

Anders Gustafsson
Anders Gustafsson

Reputation: 15981

I think you can find the answer to your issue on the NLog Codeplex forum, here.

If you initialize the static logger directly in the field declaration inside your Form1 form, the Form1 instance will not yet exist, and NLog will go on creating a new form for the RichTextBox target.

What you need to do is delay the initialization of the logger to a time when the Form1 instance is already initialized, for example in a Load event handler.

Here is an excerpt of functional code from the Codeplex issue:

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();
    }
}

To avoid unnecessary re-initialization, you might want to initialize logger only if it has not already been initialized, i.e.

    private void Form1_Load(object sender, EventArgs e)
    {
        if (logger == null) logger = LogManager.GetCurrentClassLogger();
    }

Upvotes: 11

Related Questions