NLog control to an existing RichTextBox Windows Form
Solution 1
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();
}
Solution 2
Here are three tips to help you load log in existing RichTextBox.
- Ensure formName and controlName is consistent with the actual use
- Set
allowAccessoryFormCreation="False"
in NLog.config -
RichTextBoxTarget.ReInitializeAllTextboxes(this);
in Form_Load
PS:Follow this way, you neeed to config your RichTextBoxTarget in NLog.config, please refer RictTextBoxTarget.
Solution 3
Don't forget to add
LogManager.ReconfigExistingLoggers();
if setting the richtextbox as a logger programmatically
Solution 4
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
Related videos on Youtube
Alvin
Updated on June 04, 2022Comments
-
Alvin almost 2 years
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.
-
Alvin almost 12 yearsException at Program.cs The type initializer for Form1.Form1' threw an exception.
-
Anders Gustafsson almost 12 years@KelvinFixx There is an old issue reported at the NLog Codeplex site regarding exception in type initializer for the
RichTextBox
target. The issue appears to have been solved in 2.0. Are you using a recent version of NLog? -
Anders Gustafsson almost 12 yearsIf I understand the NLog documentation correctly you should be able to link
formName
to an existingForm
in your application. Have you tried this? BTW, is your application WPF or Windows Forms? -
Alvin almost 12 yearsI have solved the problem, changing form to formName. Now the problem is it create it's own form.
-
Anders Gustafsson almost 12 yearsAnd it will not work to set
formName
andcontrolName
to a form and control already existing in your application? Anyway, to help other readers, it would be really good if you could write up (and accept) your own answer, i.e. thatform
should beformName
. Alternatively, rephrase the question to emphasize that you would like to attach the NLog control to an existingForm
. -
Alvin almost 12 yearswindows form, my form name is Form1.
-
Anders Gustafsson almost 12 yearsAccording to the documentation the form has to be open before you attach the NLog target. Perhaps you need to create your target programmatically, as I assume the NLog configuration is executed before UI component initialization?
-
Alvin almost 12 yearsYou might be right, then it defect the purpose of having config file.
-
Alvin almost 12 years@Andres Gustafsson Yeh you are right! nlog.codeplex.com/workitem/2707
-
Anders Gustafsson almost 12 years@KelvinFixx I found it too. I was just giving the answer a final touch when I saw your comment :-)
-