4
任何人都可以提供一个非常示例自定义layoutrenderer nlog?Nlog自定义layoutrenderer
我想使缩进,而IM日志记录,例如
如果IM调用方法B,方法C
文本日志文件是这样的:
Inside Method C
Inside Method B
等。
任何人都可以提供一个非常示例自定义layoutrenderer nlog?Nlog自定义layoutrenderer
我想使缩进,而IM日志记录,例如
如果IM调用方法B,方法C
文本日志文件是这样的:
Inside Method C
Inside Method B
等。
那就是:
[LayoutRenderer("IndentationLayout")]
public sealed class IndentationLayoutRenderer : LayoutRenderer
{
// Value to substract from stack count
private uint _ignore = 12;
// Value to pad with.
private string _ipadding = "| ";
/// <summary>Set the number of (top) stackframes to ignore</summary>
public uint Ignore
{
get { return _ignore; }
set { _ignore = value; }
}
/// <summary>Set the padding value</summary>
public string IndentationPadding
{
get { return _ipadding; }
set { _ipadding = value; }
}
protected override void Append(StringBuilder builder, LogEventInfo ev)
{
// Get current stack depth, insert padding chars.
StackTrace st = new StackTrace();
long indent = st.FrameCount;
indent = indent > _ignore ? indent - _ignore : 0;
for (int i = 0; i < indent; ++i)
{
builder.Append(_ipadding);
}
}
}
报名:
if(NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.AllRegisteredItems.ContainsKey(
"IndentationLayout"))
return;
NLog.Config.ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("IndentationLayout", typeof(IndentationLayoutRenderer));
你知不知道我是否能配置nlog.config文件LayoutRenderers? – 2011-09-28 14:17:26
由于在优化过程中发生代码转换,因此StackTrace可能不会报告预期的方法调用。[ - MSDN。](http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stacktrace .aspx)我会考虑创建静态的Indent,UnIndent方法,它需要一个Logger以及一个'private static Dictionary'。然后在Append中,使用LogEventInfo.LoggerName检查字典以获取IndentLevel。 –
2012-01-26 21:39:55