NLog:如何使用函数(纯代码)代替布局字符串? [英] NLog: How to use function (plain code) instead of layout string?
本文介绍了NLog:如何使用函数(纯代码)代替布局字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过声明性布局字符串指定消息格式是一种简便的方法,但仅在简单情况下.
Specifying message format via declarative layout string is a easy way but only in simple cases.
当我看到条件或嵌套布局时,我想使用普通代码来格式化消息.
When I see conditions or nested layouts I want to use plain code to format my message.
如何通过普通代码格式化邮件?
How can I format my messages via plain code?
推荐答案
您还可以创建自己的自定义布局类:
You can also create your own custom Layout-class:
class CustomLayout : Layout
{
protected string GetFormattedMessage(LogEventInfo logEvent)
{
// Legacy-style
return "King of the world";
}
protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
{
// New style supports reusable StringBuilder (reduces allocation)
target.Append(GetFormattedMessage(logEvent));
}
}
这篇关于NLog:如何使用函数(纯代码)代替布局字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文