启用日志记录到 log4net 中的两个不同位置

发布于 2024-12-19 21:27:14 字数 834 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2024-12-26 21:27:14

理想情况下,应该发生的是:

  1. 配置 log4net(在应用程序启动时执行此操作一次 - 这非常慢)
  2. 使用依赖项注入将 ILog 传递给类(更好) 服务定位器(更糟糕)获取所需类中的 ILog
  3. 一个配置可以为您提供多个记录器,而这些记录器又可以有多个附加程序,这些附加程序可以记录到各种输出(数据库、控制台、队列等),这为您提供了很多的灵活性管理通过一个配置文件。您可以拥有一个配置文件,其中包含两个具有不同名称的文件记录器,它们将使用两个不同的附加程序将数据输出到两个文件中。

该代码

private  ILog Log  {
    get {
        if (!_ConfiguratorSet) {
            _ConfiguratorSet = true;
            XmlConfigurator.Configure(new FileInfo(_ConfigFile)); //<--- STATIC
        }
        return _log;
    }
}

确实令人困惑且错误。你应该只在那里返回一个日志,所以根本没有if。这违反了单一责任原则,也可能是您看到的错误的原因。

Ideally, what shall be happening is:

  1. Configure log4net (do this once at app start up - this is very slow)
  2. Use dependency injection to pass ILog to the classes (better) or service locator (worse) to get the ILog in the needed class
  3. One configuration can provide you with multiple loggers, which in turn can have multiple appenders that can log to various outputs (db, console, queues etc.) This gives you a lot of flexibility governed by one config file. You can have one config file with two file loggers with different names that will use two different appenders to output data, well, into two files.

The code

private  ILog Log  {
    get {
        if (!_ConfiguratorSet) {
            _ConfiguratorSet = true;
            XmlConfigurator.Configure(new FileInfo(_ConfigFile)); //<--- STATIC
        }
        return _log;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文