布局中的 nlog 子字符串

发布于 2024-12-26 15:34:44 字数 809 浏览 3 评论 0原文

有没有办法将消息的子字符串发送到 nlog 布局? 像 ${${substring(0,200)}message} 这样的东西

        <parameter name="@Summary" layout="${substring(0,200)}${message}"/>

如果能写出 message.substring(0, 200); 的等价物那就太酷了

    <target type="Database" name="databaseLog" 
             ConnectionStringName="ApplicationConnectionString">
        <commandText>
            INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
            VALUES            (@Description,  @Summary,  @Level  )
        </commandText>

        <parameter name="@Description" layout="${message}"/>
        <parameter name="@Summary" layout="{{SUBSTRING-OF-MESSAGE-HERE}}"/>
        <parameter name="@Level" layout="${level}"/> 
    </target> 

is there a way to get a substring of the message to the nlog layout?
something like ${${substring(0,200)}message}

        <parameter name="@Summary" layout="${substring(0,200)}${message}"/>

It would be cool if that would write the equlivent of message.substring(0, 200);

    <target type="Database" name="databaseLog" 
             ConnectionStringName="ApplicationConnectionString">
        <commandText>
            INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
            VALUES            (@Description,  @Summary,  @Level  )
        </commandText>

        <parameter name="@Description" layout="${message}"/>
        <parameter name="@Summary" layout="{{SUBSTRING-OF-MESSAGE-HERE}}"/>
        <parameter name="@Level" layout="${level}"/> 
    </target> 

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

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

发布评论

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

评论(3

小兔几 2025-01-02 15:34:44

您可以使用 ${pad} 布局渲染器:

layout=${pad:padding=-200:fixedLength=true:inner=${message}}

正填充值导致左侧填充,负值导致右侧填充到所需的宽度。 文档

或者尝试使用 T-SQL SUBSTR:

<commandText>
  INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
  VALUES (@Description,  SUBSTR(@Summary, 1, 200),  @Level)
</commandText>

You may use ${pad} layout renderer:

layout=${pad:padding=-200:fixedLength=true:inner=${message}}

Positive padding values cause left padding, negative values cause right padding to the desired width. Documentation

Or try to use T-SQL SUBSTR:

<commandText>
  INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
  VALUES (@Description,  SUBSTR(@Summary, 1, 200),  @Level)
</commandText>
醉生梦死 2025-01-02 15:34:44

NLog 4.6.3 现在可以执行子字符串逻辑:

https://github.com /NLog/NLog/wiki/Substring-layout-renderer

并截断:

${mesage:truncate=80}

NLog 4.6.3 can now perform substring-logic:

https://github.com/NLog/NLog/wiki/Substring-layout-renderer

And truncate:

${mesage:truncate=80}
长伴 2025-01-02 15:34:44

试试这个。我遇到了同样的问题,最终写下了这篇文章。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Your.NameSpace
{
    [NLog.LayoutRenderers.LayoutRenderer("shorten")]
    [NLog.LayoutRenderers.AmbientProperty("HeadLength")]
    [NLog.LayoutRenderers.AmbientProperty("TailLength")]
    [NLog.LayoutRenderers.AmbientProperty("WarnMsg")]
    public class ShortenRendererWrapper : NLog.LayoutRenderers.Wrappers.WrapperLayoutRendererBase
    {


        [System.ComponentModel.DefaultValue(500)]
        public int HeadLength { get; set; }

        [System.ComponentModel.DefaultValue(500)]
        public int TailLength { get; set; }

        [System.ComponentModel.DefaultValue(true)]
        public bool ShowWarnMsg { get; set; }

        [System.ComponentModel.DefaultValue("###_SHORTENED_###")]
        public string WarnMsg { get; set; }

        public ShortenRendererWrapper()
        {
            HeadLength = 500;
            TailLength = 500;
            ShowWarnMsg = true;
            WarnMsg = "###_SHORTENED_###";
        }

        protected override string Transform(string text)
        {
            if (text.Length > (HeadLength + TailLength))
            {
                if (HeadLength > 0 && TailLength > 0)
                    return text.Substring(0, HeadLength)
                        + (ShowWarnMsg ? "<........." + WarnMsg + ".........>" : string.Empty)
                        + text.Substring(text.Length - TailLength, TailLength);
                else if (HeadLength > 0)
                    return text.Substring(0, HeadLength)
                        + (ShowWarnMsg ? "<........." + WarnMsg + ">" : string.Empty);
                else if (TailLength > 0)
                    return (ShowWarnMsg ? "<" + WarnMsg + ".........>" : string.Empty)
                        + text.Substring(text.Length - TailLength, TailLength);
                else
                    return text;
            }
            else
                return text;
        }        
    }
}

它是一个包装渲染器。通过在 nlog 配置文件中添加“扩展”标签来使用,如下所示,

<nlog>
    <extensions>
        <add assembly="Your.Namespace"/>
    </extensions>
    <targets>
        <target layout="${shorten:headLength=1000:tailLength=200:inner=${message})"/>
    </targets>
</nlog>

这将为您提供从日志消息开头算起的 1000 个字符和从结尾算起的 200 个字符,并在中间显示一条警告,通知字符串不完整。
您可以通过设置“warnMsg”值来定制警告消息,或者通过将“showWarnMsg”设置为“false”来完全关闭它。
'headLength' 和 'tailLength' 可以单独使用来给出从字符串的开头或结尾开始的子字符串。如果没有给出任何值或者字符串比指定值的总和短,则字符串将按原样返回。

Try this. I had the same problem and ended up writing this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Your.NameSpace
{
    [NLog.LayoutRenderers.LayoutRenderer("shorten")]
    [NLog.LayoutRenderers.AmbientProperty("HeadLength")]
    [NLog.LayoutRenderers.AmbientProperty("TailLength")]
    [NLog.LayoutRenderers.AmbientProperty("WarnMsg")]
    public class ShortenRendererWrapper : NLog.LayoutRenderers.Wrappers.WrapperLayoutRendererBase
    {


        [System.ComponentModel.DefaultValue(500)]
        public int HeadLength { get; set; }

        [System.ComponentModel.DefaultValue(500)]
        public int TailLength { get; set; }

        [System.ComponentModel.DefaultValue(true)]
        public bool ShowWarnMsg { get; set; }

        [System.ComponentModel.DefaultValue("###_SHORTENED_###")]
        public string WarnMsg { get; set; }

        public ShortenRendererWrapper()
        {
            HeadLength = 500;
            TailLength = 500;
            ShowWarnMsg = true;
            WarnMsg = "###_SHORTENED_###";
        }

        protected override string Transform(string text)
        {
            if (text.Length > (HeadLength + TailLength))
            {
                if (HeadLength > 0 && TailLength > 0)
                    return text.Substring(0, HeadLength)
                        + (ShowWarnMsg ? "<........." + WarnMsg + ".........>" : string.Empty)
                        + text.Substring(text.Length - TailLength, TailLength);
                else if (HeadLength > 0)
                    return text.Substring(0, HeadLength)
                        + (ShowWarnMsg ? "<........." + WarnMsg + ">" : string.Empty);
                else if (TailLength > 0)
                    return (ShowWarnMsg ? "<" + WarnMsg + ".........>" : string.Empty)
                        + text.Substring(text.Length - TailLength, TailLength);
                else
                    return text;
            }
            else
                return text;
        }        
    }
}

It is a wrapper renderer. Use by adding a 'extensions' tag to your nlog config file like

<nlog>
    <extensions>
        <add assembly="Your.Namespace"/>
    </extensions>
    <targets>
        <target layout="${shorten:headLength=1000:tailLength=200:inner=${message})"/>
    </targets>
</nlog>

This will give you 1000 chars from the beginning and 200 chars from the end of the log message with a warning in the middle that notifies the string is not complete.
You can tailor the warning message by setting the 'warnMsg' value or turn it completely off by setting 'showWarnMsg' to 'false'.
'headLength' and 'tailLength' can be used alone to give a substring from the begining or the end of the string. If neither value is given or if the string is shorter than the sum of the value(s) specified, the string will be returned as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文