Log4net main components
1. Loggers
-
- A logger is the portion of the file that calls the LogManager to retrieve an instance of a specific logger that has been used to write information. Example:
private readonly ILog log = LogManager.GetLogger(typeof(AdjudicateClaimHandler));
log.InfoFormat("Claim Id {0}: Started", message.InternalId);
-
- There are 5 standard levels of logging within the log4net file:
1. DEBUG
This level of logging is used mostly for development and testing.
2. INFO
Used to output information useful to the running and management of the system. This is used mostly for entry and exit points of a given process.
3. WARN
This is used for handling exceptions where processing is able to continue safely.
4. ERROR
This level of logging is used for unhandled exceptions and errors that are mostly caught in a try catch block.
5. FATAL
This is used for severe errors that cause premature termination.
2. Appenders
-
- Appenders help to define the output destination of a log message itself. They also can filter events based on level or loggers names that are used. These filters can also be configured to accept or reject an event based upon matching. The filter element of an appender is to be placed in the appender node.
3. Layouts
-
- Layouts allow the application to customize the format. There are several out of the box layouts available. Typically, most layouts use a PatternLayout with a conversion pattern to define the output format.
- For more information on these PatternLayouts: https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Layout.PatternLayout.html
- The standard pattern layout used by QC logs the date time stamp, thread, logging level, the logger name, then prints the message on the following line. It is configured as follows (and also note the use of tabs and spaces):
-
-
- %d is equivalent to date (IOWs, you can configure as %date as well as %d, they are interchangeable.)
- %t is equivalent to thread. Notice that the %t is in brackets. The brackets are literal so your thread will pint within the brackets as [4]. The same can be said for spacing, tabs, returns, etc…
- %-5p The ‘p’ is the equivalent of logging level. You can also specify %level. The -5 is called a format modifier. This particular modifier will left justify and right pad with spaces if the element is less than 5 spaces. Modifiers can also be used to truncate long names.
- %c is the short hand equivalent for %logger. It displays the name of the fully qualified name of the logger. The logger name is typically set from the LogManager by the class type. The logger name can use a precision qualifier to shorten names. For example, if the logger name is “a.b.c.d” the patter %logger{2} will output “c.d”.
- %n line break
- %m or %message is the actual log message
-
Log4net Files – Basic Configuration
App.config must contain the following:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
Remove any other logging section and\or elements.
The configuration needs a log4net element which will contain your appender definitions. The following example contains 3 appenders for console, event viewer, and rolling log file:
<log4net>
<!--Colored console appender used for debugging in visual studio -->
<appender name="DebugConsole" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="INFO" />
<foreColor value="Green, HighIntensity" />
</mapping>
<mapping>
<level value="NOTICE" />
<foreColor value="Cyan, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="FATAL" />
<backColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c%n %m%n" />
</layout>
</appender>
<!--This is the rolling file appender. The file name and path is coded-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\Logs\ServiceLog.txt" />
<datePattern value="yyyy-MM-dd" />
<appen dToFile value="true" />
<maximumFileSize value="2048KB" />
<maxSizeRollBackups value="10" />
<rollingStyle value="Composite" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d [%t] %-5p %c%n %m%n" />
</layout>
</appender>
<root>
<!--the root level logging threshold and default appender-->
<level value="DEBUG" />
<appender-ref ref="DebugConsole" />
</root>
<logger name="NHibernate">
<!--this will set the threshold for NHibernate to WARN.-->
<level value="WARN" />
</logger>
<logger name="NServiceBus.Serializers">
<!--This will set threshold for serializer to WARN.-->
<level value="WARN" />
</logger>
</log4net>
The highlighted portion in the log4net\root element defines which appender to use. If you wanted to switch from the console to the rolling file, you would replace the appender name from DebugConsole to RollingFileAppender. Or, you could add multiple appenders such as the RollingFileAppender and the EventLogAppender for a production environment.
The ColoredConsoleAppender color mappings allow for color coded levels:
Comments
0 comments
Please sign in to leave a comment.