Using log4net in Web Applications
Join the DZone community and get the full member experience.
Join For Free Log4net is the package containing a dll. this dll contains a class logger, which is used to log the messages. It will help the programmer to output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary. The log4net package is designed so that log statements can remain in shipped code without incurring a high performance cost. It follows that the speed of logging (or rather not logging) is crucial.
Log4naet will help you to collect errors in following ways:
• FileAppender:
Using this you can save messages in file
• SMTPAppender:
using this you can save the messages in mail
• ADONetAppender:
Using this you can save the messages in Database
• EventLogAppender:
this you can save the messages in Event Viewer
In following example we are going to use ‘FileAppender’ mode which help us to print all messages in file. The file will be appended rather than overwritten each time when the logging initiated.
Configuration and setup
1) Create your .net application
2) Log4Net consists of only one DLL. Download the DLL file from following link: http://logging.apache.org/log4net/download.html
(Please, remember to download log4net.dll file as per your .NET framework)
3) After downloading extract the file.add its reference in your Application Project.
4) Create a new config file in the root of your Application. Name it Log4Net.config and paste the following code into it.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <threshold value="INFO" /> <file value="Log\[applicationname].log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="'.'yyyyMMdd'.log'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger %X{user} %X{url} - %message%newline" /> </layout> </appender> <appender name="SmtpAppenderNotify" type="log4net.Appender.SmtpAppender"> <!--Set threshold for this appender--> <threshold value="INFO" /> <to value="[someone]@[somewhere.com]" /> <from value="[someone]@[somewhere.com]" /> <subject value="Error from [applicationname]" /> <smtpHost value="[100.100.100.100]" /> <bufferSize value="1" /> <lossy value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date - %message%newline" /> </layout> </appender> <root> <level value="Error" /> </root> <logger name="Notify"> <!--Set level for this logger--> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="SmtpAppenderNotify" /> </logger> </log4net> </configuration> In this config file <file value="Log\[applicationname].log" />
It’s nothing but the path of log file which will be created in your root of Application
5) You will need to add the following line to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
5) To log the messages in code window
• First we have to import the namespace log4net to use log class
using log4net;
• Create the object of Logger At the top of class
ILog Log = LogManager.GetLogger("Notify");
• Once you’ve declared the logger, you can call one its logging methods.
Log.Info("Page Loaded.........");
using log4net;
public partial class _Default : System.Web.UI.Page { ILog Log = LogManager.GetLogger("Notify"); protected void Page_Load(object sender, EventArgs e) { Log.Info("Page Loaded........."); Log.Error("Page Loaded........."); Log.Warn("Page Loaded........."); Log.Debug("Page Loaded........."); } }
6) There are different types for logs. So it’s all up to you whether you want to display log message as INFO or ERROR, WARN, DEBUG, INFO, etc.
7) For using log4net in Web applications/ Web Service same process can be followed for logging
8) Logging errors in single file:
By giving same log file path we can log all errors (ie from win application / web service / web application) in a single file
Locking and unlocking of that shared log file will be handled by following way in log4net.config.
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
This I already put up in our log4net.config file
The goal for this blog is to not only inform the development community about using log4net in .NET applications, but also to provide an easy to use, step by step process for implementation. We hope that this article accomplished its goal, please feel free to leave any comments and we will do our best to get back to you.
This article was written by the Imaginovation team. They are a Raleigh web design and software development company who uses .NET, PHP, HTML5, JavaScript, and jQuery technologies.
Published at DZone with permission of Michael Georgiou. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments