2012-01-13 13 views
3

nlogレイアウトにメッセージの部分文字列を取得する方法はありますか?それはmessage.substringのequlivent(0、200)を記述した場合${${substring(0,200)}message}レイアウトのnlogサブストリング

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

よう 何かがそれはクールになります。

<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> 

答えて

4

あなたは$ {}パッドレイアウトレンダラーを使用することができる:

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> 
+0

Sqlサブストリングがありがたいです – eiu165

+0

SUBSTRはSQLのSUBSTRING(@Summary、1、200)である必要があります – eiu165

+0

$ {pad}の問題は、文字列がパディング長よりも小さい場合、実際に空白で埋められるということです。したがって、部分文字列ではありません。 TSQLソリューションは一般的に対象には適用されません。 – AlexP

0

はこれを試してみてください。私は同じ問題を抱え、これを書いてしまった。

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

ラッパーレンダラーです。これは、文字列を通知真ん中に警告してあなたのログメッセージの終わりから初めから1000の文字と200個の文字を与える

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

のようなあなたのnlog設定ファイルに「伸長」タグを追加することにより、使用完了していません。 'warnMsg'値を設定することで警告メッセージを調整したり、 'showWarnMsg'を 'false'に設定して警告メッセージを完全に無効にすることができます。 'headLength'と 'tailLength'を単独で使用して、文字列の先頭または末尾から部分文字列を得ることができます。値が指定されていない場合、または文字列が指定された値の合計よりも短い場合、文字列はそのまま返されます。