我有这段代码
93.200.125.74.in-addr.arpa. IN PTR youtube-ui.l.google.com.)
public partial class MainForm : Telerik.WinControls.UI.RadForm
{
private static Logger logger = LogManager.GetLogger("Main");
private MyClass=new MyClass();
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
SetupFormLogger();
logger.Info(Properties.LogMessages.AppStarted); // this message appears in the richtextbox log
}
public void SetupFormLogger()
{
NLog.Windows.Forms.RichTextBoxTarget target = new NLog.Windows.Forms.RichTextBoxTarget();
target.Name = "control";
target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}";
target.ControlName = richtextLog.Name;
target.FormName = this.Name;
target.TargetForm = this;
target.AutoScroll = true;
target.MaxLines = 10000;
target.UseDefaultRowColoringRules = false;
target.RowColoringRules.Add(
new RichTextBoxRowColoringRule(
"level == LogLevel.Trace", // condition
"WhiteSmoke", // font color
"Black", // background color
FontStyle.Regular
)
);
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "WhiteSmoke", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "DarkRed", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Error", "White", "DarkRed", FontStyle.Bold));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "Yellow", "DarkRed", FontStyle.Bold));
AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper();
asyncWrapper.Name = "AsyncRichTextBox";
asyncWrapper.WrappedTarget = target;
SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Debug);
}
}
答案 0 :(得分:1)
我认为订单很重要。在设置日志记录目标之前创建MyClass。我认为有一个像ReconfigureExistingLoggers
这样的函数或类似的函数...在创建日志记录目标后调用它或者调高setup函数的调用并将MyClass创建移动到FormLoad ...
编辑: 只是使用ReconfigureExistingLoggers不会有帮助,因为之前创建了MyClass gots,所以在有任何LoggingTarget之前发送了日志消息。
EDIT2:你在哪里打电话给MyClass.Test
?
EDIT3:我不知道你方有什么不对,我只是将你的代码复制到一个空表格,添加了一个RichTextBox和一个Button,稍微修改了你的代码(日志消息的内容,而不是硬编码记录器名称)到这个它的工作原理。我使用的是NLog 4.4.12和NLog.Windows.Forms 4.2.3
public partial class Form1 : Form
{
private static Logger logger = LogManager.GetLogger(typeof(Main).Name);
private MyClass instance =new MyClass();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetupFormLogger();
logger.Info("Some message"); // this message appears in the richtextbox log
}
public void SetupFormLogger()
{
NLog.Windows.Forms.RichTextBoxTarget target = new NLog.Windows.Forms.RichTextBoxTarget();
target.Name = "control";
target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}";
target.ControlName = richTextBox1.Name;
target.FormName = this.Name;
target.TargetForm = this;
target.AutoScroll = true;
target.MaxLines = 10000;
target.UseDefaultRowColoringRules = false;
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Trace", "WhiteSmoke", "Black", FontStyle.Regular));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "WhiteSmoke", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "DarkRed", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Error", "White", "DarkRed", FontStyle.Bold));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "Yellow", "DarkRed", FontStyle.Bold));
AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper();
asyncWrapper.Name = "AsyncRichTextBox";
asyncWrapper.WrappedTarget = target;
SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Debug);
}
private void button1_Click(object sender, EventArgs e)
{
instance.Test();
}
}
class MyClass
{
public void Test()
{
Logger log = LogManager.GetLogger(typeof(MyClass).Name);
log.Info("Text"); // THIS TEXT IS NOT LOGGED OR DISPLAYED IN RICHTEXTBOX
}
}