Nlog写日志到RichTextBox错误

时间:2015-01-06 09:29:04

标签: c# richtextbox nlog

我在我的项目中使用Nlog,现在出现了问题,这里是: 我想将日志写入RichTextBox,一开始一切正常,但是当我清除RTB中的行,然后重新开始写日志时,它会出错,它不会在RTB中显示任何行。 但是,如果我使用

log.Error("Error\n");  // there is a "\n" in the end

而不是

log.Error("Error"); 

没关系,我不知道为什么...... 这个的任何线索?非常感谢任何建议。

BTW我使用的是Win7 X64,VS 2013,C#WinForm,.NET 4.0,NLog 3.2.0

using System;
using System.Windows.Forms;

using NLog;
using NLog.Targets;

namespace RTBLogDemo
{
    public partial class LogForm : Form
    {
        #region Designer

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.logRichTextBox = new System.Windows.Forms.RichTextBox();
            this.btnStart = new System.Windows.Forms.Button();
            this.btnStop = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.logTimer = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // logRichTextBox
            // 
            this.logRichTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.logRichTextBox.Location = new System.Drawing.Point(0, 173);
            this.logRichTextBox.Name = "logRichTextBox";
            this.logRichTextBox.Size = new System.Drawing.Size(656, 299);
            this.logRichTextBox.TabIndex = 0;
            this.logRichTextBox.Text = "";
            // 
            // btnStart
            // 
            this.btnStart.Location = new System.Drawing.Point(100, 48);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(75, 39);
            this.btnStart.TabIndex = 1;
            this.btnStart.Text = "start";
            this.btnStart.UseVisualStyleBackColor = true;
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            // 
            // btnStop
            // 
            this.btnStop.Location = new System.Drawing.Point(261, 48);
            this.btnStop.Name = "btnStop";
            this.btnStop.Size = new System.Drawing.Size(75, 39);
            this.btnStop.TabIndex = 2;
            this.btnStop.Text = "stop";
            this.btnStop.UseVisualStyleBackColor = true;
            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(417, 48);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(75, 39);
            this.btnClear.TabIndex = 3;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // logTimer
            // 
            this.logTimer.Tick += new System.EventHandler(this.logTimer_Tick);
            // 
            // LogForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(656, 472);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnStop);
            this.Controls.Add(this.btnStart);
            this.Controls.Add(this.logRichTextBox);
            this.Name = "LogForm";
            this.Text = "Log Form";
            this.Load += new System.EventHandler(this.LogForm_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox logRichTextBox;
        private System.Windows.Forms.Button btnStart;
        private System.Windows.Forms.Button btnStop;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Timer logTimer;

        #endregion

        public LogForm()
        {
            InitializeComponent();
        }

        private Logger log;

        private void logTimer_Tick(object sender, EventArgs e)
        {
            log.Error("Error");     // WRONG
            // log.Error("Error\n");     // OK
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            logTimer.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            logTimer.Stop();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            logRichTextBox.Clear();

        }

        private void InitLogger()
        {
            RichTextBoxTarget target = new RichTextBoxTarget();
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            target.ControlName = logRichTextBox.Name;
            target.FormName = this.Name;
            target.UseDefaultRowColoringRules = true;
            target.AutoScroll = true;
            target.MaxLines = 50;

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
            log = LogManager.GetCurrentClassLogger();
        }

        private void LogForm_Load(object sender, EventArgs e)
        {
            InitLogger();
        }
    }
}

0 个答案:

没有答案