有没有办法在NLog布局中进行条件化?

时间:2010-07-06 22:36:24

标签: xml logging configuration layout nlog

我正在尝试创建一个NLog布局配置,如下所示:

layout = "${callsite} > ${message} (${exception:format=tostring})"

此布局的输出是(当我测试时):

  

TestProject.Program.Main>抛出异常(System.Exception:异常消息)

但是,如果我尝试输出没有异常的消息,它会显示如下:

  

TestProject.Program.Main>没有异常抛出()

有没有办法修改这个布局,以便只有在实际出现异常时才显示括号?

1 个答案:

答案 0 :(得分:0)

您可以像这样编写自己的LayoutRenderer:

  [LayoutRenderer("MyCustomExceptionLayoutRenderer")]
  class MyCustomExceptionLayoutRenderer : LayoutRenderer
  {
    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {
      if (logEvent.Exception == null)
      {
        builder.Append("no exception thrown");
      }
      else
      {
        //Might want fancier formatting of exception here...
        builder.Append("exception thrown ({0})", logEvent.Exception.ToString());
      }
    }

    protected override int GetEstimatedBufferSize(LogEventInfo logEvent)
    {
      return 100;
    }
  }

我认为(但还没试过)你也可以定义你的LayoutRenderer来接受一个(或多个参数):

  public enum BracketOption
  {
    None,
    Parentheses,        // ()
    CurlyBraces,        // {}
    SquareBraces,       // []
    LessThanGreaterThan // <>
  }

  [LayoutRenderer("MyCustomExceptionLayoutRenderer")]
  class MyCustomExceptionLayoutRenderer : LayoutRenderer
  {
    public BracketOption Option { get; set; }

    private string left = "";
    private string right = "";

    public MyCustomExceptionLayoutRenderer()
      :base()
    {
      Option = BracketOption.None;
    }

    protected override void Initialize()
    {
      switch(Option)
      {
        case BracketOption.Parentheses:
          left = "(";
          right = ")";
          break;
        case BracketOption.CurlyBraces:
          left = "{";
          right = "}";
          break;
        case BracketOption.SquareBraces:
          left = "[";
          right = "]";
          break;
        case BracketOption.LessThanGreaterThan:
          left = "<";
          right = ">";
          break;
        case BracketOption.None:
        default:
          left = "";
          right = "";
          break;
      }
    }

    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {
      if (logEvent.Exception == null)
      {
        builder.Append("no exception thrown");
      }
      else
      {
        //Might want fancier formatting of exception here...
        builder.Append(string.Format("exception thrown {0}{1}{2}", 
                        left, logEvent.Exception.ToString(), right);
      }
    }

    protected override int GetEstimatedBufferSize(LogEventInfo logEvent)
    {
      return 100;
    }
  }

要使用,请将此类行添加到NLog.config(如果配置内联,则添加app.config):

<extensions>
  <add assembly="MyNLogExtensions" />
</extensions>

并添加到您的布局中:

<targets>
  <target type="Console" layout="Exception: ${MyCustomException}" />
</targets>

参数化布局可能如下所示(不确定指定选项的确切格式):

<targets>
  <target type="Console" layout="Exception: ${MyCustomException:BracketOption=Parentheses}" />
</targets>