Re: General question about logging concept

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




you can have a different logger for each assembly, or namespace, or
class. It's totally configurable. It's even configurable on the fly
so say your program is running in production and you have logging set
to Info and you need to debug a problem you can in production change
the log.config file to set the debug level for a particular namespace
or class to Debug to get more log messages (assuming you have more
messages in the code).

Also, by default the log messages include the logger info, so it's a
good way to get source info into log messages.

However, if you're building a custom Log class with all static methods
you can choose to ignore the type parameter and always get the same
logger (log4net caches loggers so repeated calls to GetLogger are
quick).
HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



On Thu, 4 Jan 2007 13:11:02 -0800, ???? ???????
<@discussions.microsoft.com> wrote:

Samuel,
Thanks for your excellent answer.
I don't understand one thing with log4net:
why to retrieve a logger u pass the assembly name?
It's not that you define a logger for each assembly in the system...
I have only one logger, why not just call it:
getLogger("file")
?

Thanks.

"Samuel R. Neff" wrote:


Once you configure logging it's configured for the entire appdomain,
not just the web dll's. The other projects in your web application
can also use the configured logging functionality.

I would recomment not putting logging information in web.config and
instead create a log.config file and configure logging from that. The
reason is log4net can watch file file and change logging parameters
on-the-fly without reloading the application. However, if you have
logging configured through web.config any changes to web.config will
cause the app to reload.

The most common approach I've seen to logging is that each class will
have an instance method defined:

private ILog log = LogManager.GetLogger(GetType());

Then within each class you can use log.Error(), log.Debug() etc.
Static method logging would have to be handled differently.

Personally I don't like this approach because it requires a lot of log
variables declared everywhere and it requires that all projects
reference the log4net dll (I believe 3rd party dll's should be
isolated to one project only whenever possible to reduce the
maintenance hassles of upgrades or migration).

What I've done is create my own static Log class which has methods
corresponding to ILog (but is not itself an ILog implementation) and
passes those calls along to log4net.

So I would have somethign like this:

public static void Error(Type t, string message)
{
LogManager.GetLogger(t).Error(message);
}

public static void Error(object t, string message)
{
Error(t != null ? t.GetType() : typeof(Log), message);
}


Which can be called from anywhere in the application. The downside is
you have to pass the type or object for each log message, but upside
is log4net is isolated and the code impact on the rest of our
application is minimal. Also one downside to this approach is
GetLogger() is called for each log message instead of simply Error()
which may have a performance penalty, although even with heavy logging
it's not noticeable (heaving as in thousands of log messages a minute
during certain debugging times). I haven't profiled the GetLogger()
calls yet but from actual experience it seems this approach is fine.

Also, the above is an oversimplification--there are actually 12
overloads each of Debug, Info, Warn, and Error.

HTH,

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



On Thu, 4 Jan 2007 02:54:00 -0800, ???? ???????
<@discussions.microsoft.com> wrote:

Hi,
I'm just now configuring some logging module in my web app (I'm using
log4net).
The web app consists of web site and multiple library projects.
Each has to log events. So, probably there is a need for a central component
(probably static one) which receive messages and log them.
I don't want each code to instantiate the logger class, this is bad practice
with respect to performance & modular design aspects.

1. If i configure my logging module in web.config, wouldn't it be available
only
to the web site?
2. Where & how would you create class that responsible for getting all the
messages and log them into log4net?
Will it be simple static class? is it belongs to the web site? external
specific project?

Thanks.



.



Relevant Pages

  • Re: Advise on using logging.getLogger needed.
    ... I have created a module (called xlogging) which sets up logging the way I want ... I found out that if I set up my logger without a name, ... Only your main script should configure the logging. ... configuring this logger will affect the configuration of all loggers ...
    (comp.lang.python)
  • Re: How to log messages _only once_ from all modules ?
    ... When I implement logging, I think that due to preparation, I get the same message more than once. ... Could you suggest what should I change in the above scripts so that the log messages would appear only once? ... never configure or add handlers to any logger except for the root logger in your __main__ section. ... self.logger.warning('This message comes from Server module ') ...
    (comp.lang.python)
  • Re: syslogd not logging problem
    ... I've tried that too and I still can't log messages to it. ... I have no idea how syslog works, but perhaps it is working fine but ... let's say that logger isn't working, it would appear that syslogd isn't ... logging, but it may not be getting any messages to log. ...
    (comp.unix.solaris)
  • Re: Simple logging example doesnt work!
    ... file handler explicitly. ... See the logging docs for more info on Filters. ... # syslog adds its own time and level stamp. ... # Set up initial logger. ...
    (comp.lang.python)
  • logging via SocketHandler and TCPserver
    ... I find that I REALLY need to be able to monitor LOTS of running programs/processes and thought it would be nice to have them use SocketHandler logging and then I would write TCPServer to accept the log messages for real-time monitoring. ... I Googled for several hours and came up with some code that I've turned in to something that works, but I can't figure out how to disconnect the server once it is connected The goal is to be able to start TCPServer, monitor the log messages sent via SocketHandler logger, disconnect, and move to the next application. ... def unPickle: ...
    (comp.lang.python)