NLog GetCurrentClassLogger slow-运行 方法警告:言过其实?
NLog GetCurrentClassLogger slow-running method warning: Overstated?
NLog 提到了这个关于 "slow-running" 的评论,我看到其他人重复这个作为警告......就好像你在使用 NLog 时应该避免使用 GetCurrentClassLogger()
一样。
我的问题是:如果您按照建议从静态字段使用 NLog,此警告是否被夸大了?难道 运行 每种类型一次...不是每个 new
一次吗?
加分项:如果那是真的,为了使此警告有效,需要怎样做才能使静态字段重复初始化?
/// <summary>
/// Gets the logger with the name of the current class.
/// </summary>
/// <returns>The logger.</returns>
/// <remarks>This is a slow-running method.
/// Make sure you're not doing this in a loop.</remarks>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.NoInlining)]
public static Logger GetCurrentClassLogger()
{
return factory.GetLogger(GetClassFullName());
}
NLog mentions this comment about "slow-running", and I've seen others repeat this as a warning... as though you should avoid using GetCurrentClassLogger()
when using NLog.
GetCurrentClassLogger
的慢速 运行 部分是 GetClassFullName
method,它扫描 StackTrace
当前的 class 名称。这不是很慢,但也不是很快,因此通过在循环中调用 GetCurrentClassLogger
来重新扫描 StackTrace
是一种浪费。需要明确的是,如果 NLog 的性能真的很重要,LogManager.GetLogger("your class name")
总是更快。
一般来说,在 class 中使用 GetCurrentClassLogger
不会对性能产生影响,即使不是 static
。出于性能原因,建议将其分配给静态字段。
My question is this: If you are using a NLog as suggested, from a static field, is this warning overstated? Wouldn't it run just once per type... not once per new
?
TL;DR:是的,您可以在使用 static
字段时忽略此警告。 "slow part" 实际上对每种类型只运行一次。
Extra credit: If that's true, what would it take to make a static field initialize repeatedly, in order to make this warning valid?
只是不要这样做:
for (int i = 0; i < 100000; i++)
{
LogManager.GetCurrentClassLogger().Trace("some cool trace");
}
NLog 提到了这个关于 "slow-running" 的评论,我看到其他人重复这个作为警告......就好像你在使用 NLog 时应该避免使用 GetCurrentClassLogger()
一样。
我的问题是:如果您按照建议从静态字段使用 NLog,此警告是否被夸大了?难道 运行 每种类型一次...不是每个 new
一次吗?
加分项:如果那是真的,为了使此警告有效,需要怎样做才能使静态字段重复初始化?
/// <summary>
/// Gets the logger with the name of the current class.
/// </summary>
/// <returns>The logger.</returns>
/// <remarks>This is a slow-running method.
/// Make sure you're not doing this in a loop.</remarks>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.NoInlining)]
public static Logger GetCurrentClassLogger()
{
return factory.GetLogger(GetClassFullName());
}
NLog mentions this comment about "slow-running", and I've seen others repeat this as a warning... as though you should avoid using
GetCurrentClassLogger()
when using NLog.
GetCurrentClassLogger
的慢速 运行 部分是 GetClassFullName
method,它扫描 StackTrace
当前的 class 名称。这不是很慢,但也不是很快,因此通过在循环中调用 GetCurrentClassLogger
来重新扫描 StackTrace
是一种浪费。需要明确的是,如果 NLog 的性能真的很重要,LogManager.GetLogger("your class name")
总是更快。
一般来说,在 class 中使用 GetCurrentClassLogger
不会对性能产生影响,即使不是 static
。出于性能原因,建议将其分配给静态字段。
My question is this: If you are using a NLog as suggested, from a static field, is this warning overstated? Wouldn't it run just once per type... not once per
new
?
TL;DR:是的,您可以在使用 static
字段时忽略此警告。 "slow part" 实际上对每种类型只运行一次。
Extra credit: If that's true, what would it take to make a static field initialize repeatedly, in order to make this warning valid?
只是不要这样做:
for (int i = 0; i < 100000; i++)
{
LogManager.GetCurrentClassLogger().Trace("some cool trace");
}