NLog自定义LayoutRenderer用于范围缩进 [英] NLog custom LayoutRenderer for scope indentation

查看:93
本文介绍了NLog自定义LayoutRenderer用于范围缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以为我提供一个非常简单的nlog自定义layoutrenderer示例吗?

can any one provide me of a very sample custom layoutrenderer for nlog ?

例如,我想在即时记录时进行缩进

I want to make indentation while im logging , by example

如果我从方法C调用方法B

if im calling Method B from Method C

文本日志文件如下所示:

the Text log file goes like this :

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 = "| ";

    /// <summary>Set the number of (top) stackframes to ignore</summary>
    public uint Ignore
    {
        get { return _ignore; }
        set { _ignore = value; }
    }

    /// <summary>Set the padding value</summary>
    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));

这篇关于NLog自定义LayoutRenderer用于范围缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆