What is Log4Net
- It is a port of logging facility from java by Apache log4jTM
When to use Log4Net
- When you want to trace user behavior.
- Analyze navigation patterns, check what are the most used sections, what filters and criteria’s are used more often.
- Resolve unexpected errors using the log information.
How to get the library
Using Nuget package installer use following command
- Install-Package log4Net
Logging Levels
There are 5 levels for logging available they are debug, info, warning, error and fatal. You can use appropriate method to log these in the application. The levels help you to analyze errors quickly and using configuration you can control what level of details needs to be logged. So choose your method appropriately.
Logging configuration
Log4Net configuration can be done using custom sections in web.config or it can be a separate configuration file. Logging can be done at various places like console, file, Windows event log, database etc. You can also restrict the amount of information to keep in these sources. Below file is basic configuration for log4net
| <?xml version=“1.0“ encoding=“utf-8“ ?>
<configuration> <configSections> <section name=“log4net“ type=“log4net.Config.Log4NetConfigurationSectionHandler, log4net“ /> </configSections> <log4net> <appender name=“LogFileAppender“ type=“log4net.Appender.FileAppender“> <param name=“File“ value=“c:EOS_LOGEOSLogFile.txt“ /> <param name=“AppendToFile“ value=“true“ /> <rollingStyle value=“Size“ /> <maxSizeRollBackups value=“10“ /> <maximumFileSize value=“10MB“ /> <staticLogFileName value=“true“ /> <layout type=“log4net.Layout.PatternLayout“> <param name=“Header“ value=“[Header]rn“ /> <param name=“Footer“ value=“[Footer]rn“ /> <param name=“ConversionPattern“ value=“%d [%t] %-5p %c %m%n“ /> </layout> </appender>
<appender name=“AdoNetAppender“ type=“log4net.Appender.AdoNetAppender“> <bufferSize value=“1“ /> <connectionType value=“System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089“ /> <connectionString value=“Data Source=[Server];Database=[Database];UID=[UserId];PWD=[Password];“ /> <commandText value=“INSERT INTO LOG_FOR_NET ([Date], [SessionId], [UserEmail]) VALUES (@log_date, @SessionId, @userEmail)“/> <parameter> <parameterName value=“@log_date“ /> <dbType value=“DateTime“ /> <layout type=“log4net.Layout.RawTimeStampLayout“ /> </parameter> <parameter> <parameterName value=“@SessionId“/> <dbType value=“String“ /> <size value=“250“ /> <layout type=“log4net.Layout.PatternLayout“> <conversionPattern value=“%property{SessionId}“ /> </layout> </parameter> <parameter> <parameterName value=“@userEmail“/> <dbType value=“String“ /> <size value=“250“ /> <layout type=“log4net.Layout.PatternLayout“> <conversionPattern value=“%property{userEmail}“ /> </layout> </parameter> </appender> <appender name=“ConsoleAppender“ type=“log4net.Appender.ConsoleAppender“ > <layout type=“log4net.Layout.PatternLayout“> <param name=“Header“ value=“[Header]rn“ /> <param name=“Footer“ value=“[Footer]rn“ /> <param name=“ConversionPattern“ value=“%d [%t] %-5p %c %m%n“ /> </layout> </appender> <root> <level value=“INFO“ /> <appender-ref ref=“AdoNetAppender“ /> </root> </log4net> </configuration> |
Notes
- You can have multiple appenders
- You can add custom fields for logging as is specified as UserEmail in above sample for AdoNetAppender
Using Log4Net
- Step 1: Read the configuration in Log4Net
- Config.XmlConfigurator.Configure();
- This statement should come as early as possible in the code. Like in application_start event
- Step 2: Creating the log object
- One log object to be created throughout the application. Use a base class if required
- private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- The object passed is current method to take additional information if required
- Step 3: Log the information
- Info(“In page load”);
- Step 4: Using custom variables
- LogicalThreadContext.Properties[“PageAccessed”] = Page.Request.Url.PathAndQuery;