开发者

Problem with my custom LogManager

开发者 https://www.devze.com 2023-03-08 17:26 出处:网络
Here is my situation: I have a library project with the aim to overriding log4net and centralize logs with a custom appender. I have these classes :

Here is my situation: I have a library project with the aim to overriding log4net and centralize logs with a custom appender. I have these classes :

myCustomLogManager
CustomLog
ICustomLog

In myCustomLogManager, i got this :

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

My custom log:

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

Into my console application project, in each class, I declare :

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

When I log in myClass, no problem. But if I use another class where i log too (let's say myOtherClass), the logger name of my future log in myClass will have the va开发者_如何学Clue "myOtherClass". To illustrate the problem, here is an example of the output :

  • [Info] myProject.myClass : "A log from myClass !"
  • [Info] myProject.myOtherClass : "A log from myOtherClass !"
  • [Info] myProject.myOtherClass : "A second log from myClass !!"

It's pretty hard to explain and i hope i'm clear enough, but the fact is the default logger, after doing stuff in myOtherClass class is myProject.myOtherClass and does not come back to myProject.myClass when i log in myClass class.

How can I fix this?

Thanks for reading and your help :-)


Make the log in CustomLog non-static:

private ILog log;

(also no need to initialize it to anything)

By having a static field you essentially just have one logger, therefore it gets changed when you use your log manager to get a "new" logger for the second class.

0

精彩评论

暂无评论...
验证码 换一张
取 消