我的自定义LogManager出现问题

本文关键字:问题 LogManager 自定义 我的 | 更新日期: 2023-09-27 17:57:53

以下是我的情况:我有一个库项目,目的是覆盖log4net并使用自定义appender集中日志。我有这些课程:

myCustomLogManager
CustomLog
ICustomLog

在myCustomLogManager中,我得到了这个:

public class myCustomLogManager
{    
    public static ICustomLog GetLogger(System.Type t)
    {
        return new CustomLog(t);
    }
    // Other stuff
}

我的自定义日志:

class CustomLog : ICustomLog
{
    private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    public CustomLog(System.Type t)
    {
        log = LogManager.GetLogger(t);
    }
    public void Debug( string message, Exception e)
    {
        log.Debug(message, e);
    }
}

在我的控制台应用程序项目中,在每个类中,我声明:

private static readonly ICustomLog logger = myCustomLogManager.GetLogger(typeof(myClass));

当我登录myClass时,没有问题。但是,如果我也使用另一个日志记录的类(比如myOtherClass),那么myClass中我未来日志的记录器名称将具有值"myOtherClass"。为了说明这个问题,这里有一个输出示例:

  • [Info]myProject.myClass:"来自myClass的日志!"
  • [Info]myProject.myOtherClass:"来自myOtherClass的日志!"
  • [Info]myProject.myOtherClass:"来自myClass的第二个日志!!"

这很难解释,我希望我足够清楚,但事实是,在myOtherClass类中做了一些事情后,默认的记录器是myProject.myOtherClass,当我登录myClass类时不会返回到myProject.myClass。

我该怎么解决这个问题?

感谢阅读和您的帮助:-)

我的自定义LogManager出现问题

使CustomLog中的log非静态:

private ILog log;

(也不需要将其初始化为任何内容)

通过有一个静态字段,您实际上只有一个记录器,因此当您使用日志管理器为第二个类获取"新"记录器时,它会发生更改。