The Video link For Log4Net How Do I? Down Load is : VideoLinkToHowDoI?[^]
- It used to work Fine in Local Machine But after Moving it to production .It is always going to error page..!
- But After some modifications in the production ,when entering into database .It is hitting the error page
- Wanted to display User Friendly Message but not the exception for users ..!But Want to store the Exception externally.
The Solution is Log4Net.
log4net is a tool to help the programmer 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 ahigh performance cost. It follows that the speed of logging (or rather not logging) is crucial.At the same time, log output can be so voluminous that it quickly becomes overwhelming. One of the distinctive features of log4net is the notion of hierarchical loggers. Using these loggers it is possible to selectively control which log statements are output at arbitrary granularity.
log4net is designed with two distinct goals in mind:
speed and flexibility
- Support for multiple frameworks
- Output to multiple logging targets
- Hierarchical logging architecture
- XML Configuration
- Dynamic Configuration
- Logging Context
- Proven architecture
- Modular and extensible design
- High performance with flexibility
log4net is configured using an XML configuration file. The configuration information can be embedded within other XML configuration files (such as the application's .config file) or in a separate file. The configuration is easily readable and updateable while retaining the flexibility to express all configurations. Alternatively log4net can be configured programmatically.
log4net can monitor its configuration file for changes and dynamically apply changes made by the configurator. The logging levels, appenders, layouts, and just about everything else can be adjusted at runtime. In many cases it is possibleto diagnose application issues without terminating the process in question. This can a very valuable tool in investigating issues with deployed applications.
How Do I Log4NetFileAppender:
Appends logging events to a file.
Start<br /> Run <br /> Devenv //<sub>Fires Up VisualStudio Developers Envoirnment</sub><br /> File<br /> NewWebSite <-
Add the Following Line in Web.config File.It Will add the Custom ConfigurationSection.To Know More About Custom Configuration
<section name="log4net "type="log4net.Config.Log4NetConfigurationSectionHandler, log4net">
Adding Custom Configuration Section
Now Add the appender configuration in the Web.configFile as shown in the following Figure.
Appender Section Image:2
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Logs\\Log4Net.log"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline"/>
Xml Code For Appender Section
For More Information about
About File Appender:
Logging events are sent to the file specified by the File property.The file can be opened in either append or overwrite mode by specifying theAppendToFile property. If the file path is relative it is taken as relative from the application base directory. The file encoding can be specified by setting theEncoding property.The layout's Header and Footer values will be written each time the file is opened and closed respectively. If the AppendToFile property is true then the file may contain multiple copies of the header and footer.This appender will first try to open the file for writing when ActivateOptions is called. This will typically be during configuration. If the file cannot be opened for writing the appender will attempt to open the file again each time a message is logged to the appender. If the file cannot be opened for writing when a message is logged then the message will be discarded by this appender.
The FileAppender supports pluggable file locking models via the Locking Model property. The default behavior, implemented by FileAppender.ExclusiveLock is to obtain an exclusive write lock on the file until this appender is closed. Thealternative model, FileAppender.MinimalLock, only holds a write lock while the appender is writing a logging event.
Global.asax File :
The Global.asax file, also known as the ASP.NET application file, is an optional filethat contains code for responding to application-level events raised by ASP.NET or by HttpModules. The Global.asax file resides in the root directory of anASP.NET-based application. At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the Http Application base class. The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.
Adding a Global.asax File:
Alt+Ctrl+L (opens SolutionExplorer) <br /> AddNewItem <br /> GlobalApplicationClass
Adds a Global.asax File in the Website.
dding a Global.asax File Image :3
Now Add the Reference of the Log4net Dll as shown in the following figure.
Alt+Ctrl+L (opens SolutionExplorer)<br /> AddReference<br /> Browse(Browse and Select the dll)
OK<- <br />
Adding Reference Of Log4Net Image :4
Add the Following line in the Application_Start event as Shown Below
Adding XmlConfigurator Image :5
void Application_Start(object sender, EventArgs e)
The Above Line instructs the XmlConfigurator to parse a configuration file and set up logging accordingly. The path to the configuration file is specified on the command line.
Application_Start event: Code that runs on application startup
Now Add the Following Code in Default.aspx Page_Load Event
log4net.ILog logger = log4net.LogManager.GetLogger("File");
logger.Info("This is For Info Message");
logger.Warn("This is For Warn Message");
logger.Error("This is For Error Message");
logger.Debug("this is for Debug Message");
Code In Default.aspx.cs Image:6
The ILog interface is use by application to log messages into the log4net framework.
Ilog Properties :
Public Instance Methods Of ILog:
|Debug||Overloaded. Log a message object with the Debug level.|
|DebugFormat||Overloaded. Log a formatted string with the Debug level.|
|Error||Overloaded. Log a message object with the Error level.|
|ErrorFormat||Overloaded. Log a formatted message string with the Errorlevel.|
|Fatal||Overloaded. Log a message object with the Fatal level.|
|FatalFormat||Overloaded. Log a formatted message string with the Fatallevel.|
|Info||Overloaded. Log a message object with the Info level.|
|InfoFormat||Overloaded. Log a formatted message string with the Infolevel.|
|Warn||Overloaded. Log a message object with the Warn level.|
|WarnFormat ||Overloaded. Log a formatted message string with the Warn |
This class has static methods that are used by a client to request a logger instance. The GetLogger method is used to retrieve a logger. For More Information on LogManager
They are many other ways to do for log4net..! For More Information on Log4net Types
Now Run The File You Will See a New Folder Logs in the Solution Explorer as Shown Below.
Logs Folder In Solution Explorer Image:7
If Still Didn't Work Please check the IIs Settings for the website .Make Sure that You gave the Write authority to the folder Logs.
Extra Features(Optional) :
You can Log the server last error in the logger file by using the
Global.asax file .
void Application_Error(object sender, EventArgs e)
log4net.ILog logger = log4net.LogManager.GetLogger("File");
string error = Server.GetLastError().GetBaseException().ToString();
The Above article helps You to log the background details of the application using log4net file appender.
- Updated on 14 October 2009