.net logging framework code example

Example 1: .net core logging level

namespace Microsoft.Extensions.Logging
{
    /// <summary>
    /// Defines logging severity levels.
    /// </summary>
    public enum LogLevel
    {
        /// <summary>
        /// Logs that contain the most detailed messages. These messages may contain sensitive application data.
        /// These messages are disabled by default and should never be enabled in a production environment.
        /// </summary>
        Trace = 0,

        /// <summary>
        /// Logs that are used for interactive investigation during development.  These logs should primarily contain
        /// information useful for debugging and have no long-term value.
        /// </summary>
        Debug = 1,

        /// <summary>
        /// Logs that track the general flow of the application. These logs should have long-term value.
        /// </summary>
        Information = 2,

        /// <summary>
        /// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the
        /// application execution to stop.
        /// </summary>
        Warning = 3,

        /// <summary>
        /// Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a
        /// failure in the current activity, not an application-wide failure.
        /// </summary>
        Error = 4,

        /// <summary>
        /// Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires
        /// immediate attention.
        /// </summary>
        Critical = 5,

        /// <summary>
        /// Not used for writing log messages. Specifies that a logging category should not write any messages.
        /// </summary>
        None = 6,
    }
}

Example 2: how to logging in framework

How do you use Log4J in your framework?
Basically it is printing/logging the important
events of the application/test run.
in my project I did logging using the log4j library.
I added the library dependency into pom.xml.
For logging we create an object from
Logger Interface and LogManager class using
getLogger method and passing the class name in it;  

private static Logger log = LogManager.getLogger(LogDemo.class.getName());
static Logger log = Logger.getLogger(log4jExample.class.getName());

We create it by passing the
name of the current class.
Then we can use this object 
to do our logging.
log.info
log.debug
log.fatal
log.error

The Logger object is responsible for capturing 
logging information and they are stored 
in a namespace hierarchy.

Tags:

C Example