tags:

views:

27

answers:

1

Hi .. just trying to get up to speed fast.

2 questions ..

My intention was to have a singleton class to make NLog available to the whole application .. is this the correct approach.

How do I record the source i.e. Class .. Method .. thread etc. of the entry in the log.

+2  A: 

What would your singleton do exactly?

One of the advantages of libraries like Nlog (and log4j etc) is that when you log, it will log with the relevant class logger - which means you can then tune the logging appropriately. If you had some singleton with just a single Logger instance, you would immediately lose this benefit. I would recommend you use the technique shown in the documentation, where each class has its own logger.

As for emitting the class name etc, look at the layout renderers page of the docs, which has many options: {callsite} and {threadid} may be what you're after.

Jon Skeet
are you saying .. create an instance of the Nlog in each class you want to log in .. then it know which class it is being called from?I though a singleton would mean that it is only instantiated once .. I have a feeling I am confused about the way to use it....
Adam
Create an instance of `Logger` in each class you want to use it in, yes... then just log through that logger. Yes, you'll end up creating more instances, but at the benefit of fine-grained configuration. It's not like it's going to load the whole assembly separately for each instance of `Logger` you build...
Jon Skeet
Jon .. I find your answers very helpful .. getting up to speed fast is frustrating .. the renderers page has solved my problem ...But by creating a separate log instance in every class I log .. what is the fin grained control I can have? are you saying basically that I can switch on logging for just a certain class .. are there any other benefits.ALSO .. seeing as you are being so helpful .. A have a single or may be 3 apps on a PC I want to log .. if I don't send them to a log file .. what way would you store them .. AND what viewer would you use to wade through the logs???
Adam
AL:SO is there a way to change the logging config while an app is running?
Adam
You know Jon .. the Renderers page .. it is blatantly obvious on the Documentation page .. how did I miss it .. I must have had my 'man eyes' on .. as my wife would say ...
Adam
@adamjellyit: More than just switching logging on and off for a particular class - you can have logging on "error only" for all classes, but "debug" for a class that you need more information for. I don't know about reconfiguring while it's running, I'm afraid - I haven't used NLog myself. Check the docs for both that and logging to other sinks (such as a database).
Jon Skeet