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