gpt4 book ai didi

c# - NLog 自定义 LayoutRenderer 用于范围缩进

转载 作者:太空狗 更新时间:2023-10-30 00:45:15 26 4
gpt4 key购买 nike

任何人都可以为我提供一个非常样本的 nlog 自定义 layoutrenderer 吗?

我想在记录时缩进,例如

如果我从方法C调用方法B

文本日志文件是这样的:

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

关于c# - NLog 自定义 LayoutRenderer 用于范围缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5951102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com