Change class name

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Change class name

Alfred
Hi! i'm wondering if there is any way to change class name of logger instance at runtime ? i want not to create logger in every class but to inject it via constructor. but after i do it i get in file wrong class name which invokes the event
Reply | Threaded
Open this post in threaded view
|

Re: Change class name

Mark H
Hi Alfred,
do you mean so that you get a unique logging name for each instance?

I've done this.

public MyObject(string instanceName)
{

logger = LogManager.GetLogger(name);

}

rather than using LogManager.GetCurrentClassLogger();

But I am not sure if creating too many of these creates additional load on logging. Does anyone have any thoughts about whether this would incur a performance hit?

Mark
Reply | Threaded
Open this post in threaded view
|

Re: Change class name

Alfred
No, i have project structure like this

public class c1
        {
                private Logger _logger = LogManager.GetCurrentClassLogger();
                public void doSmth()
                {
                        c2 myC2= new c2(_logger);
                        myC2.LogSomething();
                }

                public void LogSomething()
                {
                        _logger.Info("c1 test");
                }
        }


        public class c2
        {
                private Logger _logger;

                public c2(Logger logger)
                {
                        this._logger = logger;
                }

                public void LogSomething() {
                        _logger.Info("c2 test");
                }
        }

everything woks fine but in log file we get
2011-09-07 09:33:59.7521|INFO|c1|c1 test
2011-09-07 09:33:59.7611|INFO|c1|c2 test

as you can see both messages came from class c1 how can this problem be fixed
Reply | Threaded
Open this post in threaded view
|

Re: Change class name

Mark H
It doesn't really look like you are calling c1.LogSomething in that example. But that's the behaviour I'd expect. My understanding is that if you want separate logging names then you need to create separate instances of logger using

LogManager.GetCurrentClassLogger() which will give you logging instances named c1 and c2 as you seem to want.
or
LogManager.GetLogger("c1") and LogManager.GetLogger("c2") which would give you the same result.

Why do you want to share the logger between the two classes? You can still configure two Loggers to the same target if that's what you want.

Again, I'm newish to NLog so I'm happy to be corrected and interested to know if there are any problems with the approach of using multiple instances rather than sharing them. Given that LogManager is essentially a Factory I'd expect them all to be connected to the same LogManager, but how many is too many?? I'm not sure of that.

cheers
Mark
Reply | Threaded
Open this post in threaded view
|

Re: Change class name

Alfred
Thanks for your reply, Mark! it will be very hard to test your app if you'll create each logger instance in its own class, better inject it via contructor. In this case you'll be able to mock your logger instance.
and about question how many is too many: it cares me too...
Reply | Threaded
Open this post in threaded view
|

Re: Change class name

Jarek Kowalski
Administrator
It's generally advised to make Logger static in your class.

private static Logger logger = LogManager.GetCurrentClassLogger();

This does not make mocking easy, unfortunately, but is good for
performance and maintenance.
NLog Blog