启用日志记录到 log4net 中的两个不同位置
我是 log4net 的新手,我正在尝试维护一些使用它的遗留代码。我注意到有两个告诉 log4net 登录到不同位置的静态类会互相绊倒。
每个类都有一个静态构造函数,
static Logger() {
_Logger = new X.LoggingService.AppLogger(
X.UtilityServer.Configuration.ConfigInfo.LoggerConfigFile);
}
除了具有不同的配置值外,看起来像这样;这两个静态类都初始化相同的 AppLogger 帮助器类。要初始化的第二个类将覆盖第一个类的初始化。我想我已经将问题追踪到这里:
private ILog Log {
get {
if (!_ConfiguratorSet) {
_ConfiguratorSet = true;
XmlConfigurator.Configure(new FileInfo(_ConfigFile)); //<--- STATIC
}
return _log;
}
}
由于我绝对不必支持线程安全,我应该删除 if 语句吗?每次我需要记录某些内容时调用 XmlConfigurator.Configure
是否会非常昂贵?有更好的办法吗?此代码是使用 log4net 版本 1.2.10 编写的
I'm brand new to log4net, and I'm trying to maintain some legacy code that uses it. I've noticed that having two static classes that tell log4net to log to different locations are tripping over each other.
The classes each have a static constructor that looks like this
static Logger() {
_Logger = new X.LoggingService.AppLogger(
X.UtilityServer.Configuration.ConfigInfo.LoggerConfigFile);
}
except with different config values; both of these static classes are initializing the same AppLogger
helper class. The second class to initialize is overwriting the initialization of the first. I think I've tracked the problem down to here:
private ILog Log {
get {
if (!_ConfiguratorSet) {
_ConfiguratorSet = true;
XmlConfigurator.Configure(new FileInfo(_ConfigFile)); //<--- STATIC
}
return _log;
}
}
Since I absolutely do not have to support thread safety, should I just get remove the if statement? Would calling XmlConfigurator.Configure
every time I need to log something be prohibitively expensive? Is there a better way? This code was written using log4net version 1.2.10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,应该发生的是:
该代码
确实令人困惑且错误。你应该只在那里返回一个日志,所以根本没有if。这违反了单一责任原则,也可能是您看到的错误的原因。
Ideally, what shall be happening is:
The code
is really confusing and wrong. You should only return a log there, so no ifs at all. It is a violation of single responsibility principle and possibly the reason of the bug you see.