NLog 自定义 LayoutRenderer 用于范围缩进

发布于 2024-11-06 18:16:41 字数 187 浏览 0 评论 0原文

任何人都可以为我提供一个非常示例的 nlog 自定义布局渲染器吗?

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

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

文本日志文件将如下所示:

Inside Method C
       Inside Method B

等等。

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

I want to make indentation while im logging , by example

if im calling Method B from Method C

the Text log file goes like this :

Inside Method C
       Inside Method B

and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-11-13 18:16:41

这是:

[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));

here it is:

[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);
        }
    }
}

Registration:

if(NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.AllRegisteredItems.ContainsKey(
                "IndentationLayout"))
                return;

NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("IndentationLayout", typeof(IndentationLayoutRenderer));
清晰传感 2024-11-13 18:16:41

使用 NLog 5.0.5,您可以使用 ${scopeindent}

${scopeindent}${message}
Logger.Info("Hello No Scope");              // Will not be indented

using (NLog.ScopeContext.PushNestedState("First Scope"))
{
   Logger.Info("Hello First Scope");        // Will be indented once
   await InnerOperationAsync();
}

static async Task InnerOperationAsync()
{
    using (NLog.ScopeContext.PushNestedState("Second Scope"))
    {
        Logger.Info("Hello Second Scope");   // Will be indented twice
        await Task.Yield();
    }
}

With NLog 5.0.5 then you can use ${scopeindent}

${scopeindent}${message}
Logger.Info("Hello No Scope");              // Will not be indented

using (NLog.ScopeContext.PushNestedState("First Scope"))
{
   Logger.Info("Hello First Scope");        // Will be indented once
   await InnerOperationAsync();
}

static async Task InnerOperationAsync()
{
    using (NLog.ScopeContext.PushNestedState("Second Scope"))
    {
        Logger.Info("Hello Second Scope");   // Will be indented twice
        await Task.Yield();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文