Development

How to programmatically create log instance by Log4Net library

Abstract: Apache Log4Net framework is very popular logging library which has been used in thousands of systems. It provides several to populate the logger instance. In general, we use the XML configuration to set the logger instance property. And the framework will load and parse the configuration quickly and easily. However, this way is fixed and static. Imagine it that you have a complicate system with lots of sub plugin application. So how to log for plugin application? We will talk it in this article.

1.    Introduction
The Apache log4net library is a tool to help the programmer output log statement to a variety of persistence media. In general, the library will generate the log instance by parsing the configuration file the programmer provides. This way is easy and popular. However, the programmatic population will be useful when we develop a very big eco-system which is composed of lots of different subsystems or applications. So it is better to provide an independent log instance to help to output log. In this article, we will take the file logging for an example.

2.    Pattern Layout
At first, you need to create a pattern layout instance which will control the log text format. It will be very easy and simple like the following snippet:
PatternLayout layout = new PatternLayout(“%date{MMM/dd/yyyy HH:mm:ss,fff} [%thread] %-5level %logger %ndc – %message%newline”);
3.    Level Filter
Level Filter is to control the log level like Info or Error. In general, you can choose all level. We need use LevelMatchFilter class or LevelRangeFilter class to finish the initialization.
LevelMatchFilter filter = new LevelMatchFilter();
filter.LevelToMatch = Level.All;
filter.ActivateOptions();

4.    File Appender
The appender is the most important part for log instance. It looks like the logging executor to implement the detail logging behavior. You can choose different appender to control the target logging media. For an example, you can use the ConsoleAppender to log to console terminal. The Apache Log4Net provides quite an amount of appenders. The RollingFileAppender is one of the most popular appenders, which will help to log into the file. After you initialize the pattern layout and level filter, you need to integrate them with the appender. Besides, you need to set other properties to control the appender behavior like the following:
RollingFileAppender appender = new RollingFileAppender();
appender.File = string.Format(“{0}\\{1}”, folderPath, “common.log”);
appender.ImmediateFlush=true;
appender.AppendToFile = true;
appender.RollingStyle=RollingFileAppender.RollingMode.Date;
appender.DatePattern = “-yyyy-MM-dd”;
appender.LockingModel = new FileAppender.MinimalLock();
appender.Name = string.Format(“{0}Appender”,instanceName);
appender.AddFilter(filter);
appender.Layout = layout;
appender.ActivateOptions();

5.    Log Instance Population
After creating the appender instance, we can use the log repository to finish the final step to populate the log instance.
string repositoryName=string.Format(“{0}Repository”,instanceName);
ILoggerRepository repository = LoggerManager.CreateRepository(repositoryName);
string loggerName = string.Format(“{0}Logger”, instanceName);
BasicConfigurator.Configure(repository, appender);
After the log instance initialization, we can get the instance from the LogManager by the special log instance name. Then you can start your logging trip.
ILog logger = LogManager.GetLogger(repositoryName, loggerName);

6.    Conclusion
If you have finished all above steps, you have succeeded in the log instance programmatic population. Congratulations!

About the Author

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories