在日志中我想:
...D...
而不是
...Debug...
同样适用于Info,Warn等。我已经尝试了${level[0]}
和布局的其他变体,没有运气。
答案 0 :(得分:1)
从NLog 4.4.6开始,您也可以${level:format=FirstCharacter}
。见docs
你可以使用${when}
这样做有点麻烦:
e.g。
${when:when=level=LogLevel.Trace:inner=T:else=${when:when=level=LogLevel.Debug:inner=D:else=TODO}}
或者您可以使用一些代码添加“包装器”:
/// <summary>
/// Take the left characters
/// </summary>
/// <example>
/// ${left:${level}:Length=2} //[DefaultParameter]
/// ${left:Inner=${level}:Length=2}
/// ${level:Length=2} //[ambient]
/// </example>
[LayoutRenderer("left")]
[AmbientProperty("Left")]
[ThreadAgnostic]
public sealed class LeftLayoutRendererWrapper : WrapperLayoutRendererBase
{
/// <summary>
/// Gets or sets the length in characters.
/// </summary>
/// <value>Index</value>
/// <docgen category='Transformation Options' order='10' />
[DefaultValue(1)]
public int Length { get; set; }
/// <summary>
/// Post-processes the rendered message.
/// </summary>
/// <param name="text">The text to be post-processed.</param>
/// <returns>Substringed</returns>
protected override string Transform(string text)
{
if (text == null)
{
return null;
}
if (Length <= 0)
{
return String.Empty;
}
return text.Substring(0, Length);
}
}
注册(尽快,例如main()
或app_start()
)
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("left", typeof(MyNamespace.LeftLayoutRendererWrapper ));
用法:
${left:${level}:Length=2} //or
${level:Length=2} //"ambient way"