130 likes | 414 Views
Logging. Using Apache log4net. Introduction. Ships must keep a written log telling speed, direction, destination, etc. A kind of diary of the ship. Large programs should keep a written log telling about all the major events in the “life” of the program.
E N D
Logging Using Apache log4net Logging, using Apache log4net
Introduction • Ships must keep a written log telling speed, direction, destination, etc. • A kind of diary of the ship. • Large programs should keep a written log telling about all the major events in the “life” of the program. • Facebook produces 25TB log file per day (2009)! • 25TB = 25 000 000 000 0000 bytes • Server programs usually keeps a log telling which clients requested what service from the server – and when – and how the server responded to the request • Ordinary response • Exception(al) response Logging, using Apache log4net
Usages of logging • Monitoring a running system • Does it perform well, or does it need more resources. • Debugging • Application does not work. Maybe the log can tell us why. • Comparable to the ”black box” on an airplane • Statistics • Example WebShop: • When do customers arrive? • For how long do they shop? • How large a percentage of customers actually buy something? • Can the users find the intended way through the shop? • Etc… Logging, using Apache log4net
The Apache log4net logging framework • Apache log4net (read “log for .net”) is a widely used logging framework in C# projects • And in .NET projects in general • Log4net is ported from log4j (log for Java). • Log4net is a professional framework • It can do a lot of things, but it might be a little hard to get started. • Log4net relies on XML configuration files • Log4net is thread safe • Ready to be used with more than a single thread Logging, using Apache log4net
Apache log4net, how to install • Log4net must be installed into your solution in Visual Studio • Note that if you want to use log4net in another solution, you must install it again. • How to … • In the “Solution Explorer” right-click your solution. • Choose “Manage NuGet Packages For Solution…” • Now you’ll see a windows similar to the window to the left • Click “Online” in the left panel. • Type “log” in the top right panel to search for packages related to “logging” Logging, using Apache log4net
Getting a Logger object • The interface ILog defines what you can do with a logger. • The class LogManager keeps track of all ILog objects • ILog log = LogManage.getLog(String name) • If you call getLog(…) with the same name, you get a reference to the same ILog object • ILog log = LogManager.getLog(Type type) • Example: LogManager.getLog(typeof(SomeClass)) • Uses the full name of SomeClass as the name. • Factory method design pattern Logging, using Apache log4net
Logger hierarchy • A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger. • The hierarchy works very much in the same way as the namespace and class hierarchy in .NET. This is very convenient as we shall soon see. • Root logger is the top of the hierarchy • It alwaysexists • It cannotberetrieved by name • It always has an assignedlevel • A child logger inherits properties from its parent logger. • Source: Apache Log4net Manual - Introduction • http://logging.apache.org/log4net/release/manual/introduction.html Logging, using Apache log4net
Log4net overview • Repository • Generally the default repository is OK. • Named repositories can be created using LogManager • LogManager • Creates loggers (ILog objects) • LogManager has a number of Appenders • Appenders really does the work • Write to console, file, database, etc. • Each Appender has • ONE Layout • A number of Filters http://www.codeproject.com/Articles/19207/log4net-XmlConfigurator-Simplified Logging, using Apache log4net
Appenders • A Logger has a number of Appenders. • Appenderswrite to the console (screen), file, database, etc. • Appendersareconfigured in an XML file • Example: loggingFirst -> logconfig.xml • Appenders have a layout property • Defineshow to make a string from a log record Logging, using Apache log4net
Logginglevels • Seven levels exist (increasing priority) • ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF • The root logger + each appender has a level set • Log entries with this level or higher will be processes. • Log entries with lower levels will be ignored • Example: root logger has level INFO • Log entries level DEBUG will be ignores • The level is set in the configuration file • Example: LoggingFirst -> logconfig.xml Logging, using Apache log4net
Filter • Appenders may have (many) filters • Filters decide if the log event should be processed or thrown away • Filters form a chain • A log event is processed if it not rejected by ALL the filters in the chain • Very much like logical OR • The last filter is usually <filter type=“log4net.Filter.DenyAllFilter” /> • To stop unwanted log event from being processed. • Filters are declared in the configuration XML file • Inside the <appender> element • Example: LoggingFirst -> logconfig.xml Logging, using Apache log4net
References and further readings • Joseph Guadagno: log4netXmlConfigurator Simplified • http://www.codeproject.com/Articles/19207/log4net-XmlConfigurator-Simplified • Apache log4net http://logging.apache.org/log4net/ • Apache log4net Manual – Introduction • http://logging.apache.org/log4net/release/manual/introduction.html • Apache log4net Manual – Configuration • http://logging.apache.org/log4net/release/manual/configuration.html • Apache log4net Config Examples • http://logging.apache.org/log4net/release/config-examples.html Logging, using Apache log4net