Nlog自定义layoutrenderer
任何人都可以为我提供一个非常样本的自定义layoutrenderer用于nlog?
举例来说,我希望在im log中进行缩进
如果我从方法C调用方法B
文本日志文件如下所示:
Inside Method C Inside Method B
等等。
这里是:
[LayoutRenderer("IndentationLayout")] public sealed class IndentationLayoutRenderer : LayoutRenderer { // Value to substract from stack count private uint _ignore = 12; // Value to pad with. private string _ipadding = "| "; /// Set the number of (top) stackframes to ignore public uint Ignore { get { return _ignore; } set { _ignore = value; } } /// Set the padding value public string IndentationPadding { get { return _ipadding; } set { _ipadding = value; } } protected override void Append(StringBuilder builder, LogEventInfo ev) { // Get current stack depth, insert padding chars. StackTrace st = new StackTrace(); long indent = st.FrameCount; indent = indent > _ignore ? indent - _ignore : 0; for (int i = 0; i < indent; ++i) { builder.Append(_ipadding); } } }
注册:
if(NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.AllRegisteredItems.ContainsKey( "IndentationLayout")) return; NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("IndentationLayout", typeof(IndentationLayoutRenderer));