【问题标题】:nlog substring in layout布局中的 nlog 子字符串
【发布时间】:2019-05-02 03:29:36
【问题描述】:

有没有办法将消息的子字符串获取到 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> 

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

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

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

    正填充值导致左填充,负值导致右填充到所需宽度。 Documentation

    或者尝试使用 T-SQL SUBSTR:

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

    【讨论】:

    • SUBSTR 在 sql 中应该是 SUBSTRING(@Summary, 1, 200)
    • ${pad} 的问题在于,当字符串小于填充长度时,它实际上会填充空格。所以它不是子字符串。而TSQL方案一般不适用于该主题。
    【解决方案2】:

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

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

    并截断:

    ${mesage:truncate=80}
    

    【讨论】:

      【解决方案3】:

      试试这个。我遇到了同样的问题,最后写了这个。

      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' 可以单独使用,从字符串的开头或结尾给出子字符串。如果两个值都没有给出,或者字符串比指定值的总和短,则字符串将按原样返回。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 2017-10-18
        • 2014-05-29
        • 1970-01-01
        相关资源
        最近更新 更多