AdSensss

How to write log in file or database using log4Net

Introduction

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 production code without incurring a high 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 (and common to all of the log4x libraries) 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. There is a tight balance between these two requirements.

Background

This article assume you have basic working knowledge of C#, and ASP.NET programming.

Using the code

Step-1  Go  New Project  in visual Studio -> Select  Asp.net Web Application
            -> Give Name "WebApplication1" -> press Ok
Step-2 Select MVC and Change Authentication Type => Individual User Account -> Press OK
Step-3 Go Tools -> NuGet Package Manager -> Manage NuGet Package for solution
                           -> Browse ->Search log4net -> install Package
Step-4 Go Project -> Properties -> AssemblyInfo -> Add [assembly: log4net.Config.XmlConfigurator(Watch = true)]




                     




This might help to understand what is recorded at what level Loggers may be assigned levels.
Levels are instances of the log4net.Core.Level class.
The following levels are defined in order of increasing severity.

Step - 5 Write this code in your web.config file

<configuration>
<configSections>
         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
       <log4net>
       <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
              <file value="??Write Your logFile Path and File Name with extension like .doc,.xls??"/>
              <appendToFile value="true" />
              <rollingStyle value="Size" />
              <maxSizeRollBackups value="1" />
              <maximumFileSize value="10MB" />
              <staticLogFileName value="true" />
              <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="Date=%date [%thread] %level %logger - %message%newline"/>
              </layout>
       </appender>
       <root>
              <level value="Write Log Level" />
              <appender-ref ref="RollingFileAppender" />
       </root>
       </log4net>
</configuration>

Step - 6 wirte this Code  in your Controller 

using log4net;
using System;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public ActionResult Index()
        {            
            log.Debug("log Debug");
            log.Info("log Info");
            log.Warn("log Warn");
            log.Error("log Error");
            log.Fatal("log Fatal");
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            log.Warn(ViewBag.Message);
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            log.Warn(ViewBag.Message);
            return View();
        }
    }
}

Points of Interest
I feel very tired to find appropriate example of log4net for web application in C#.
But Finally I have completed logging module using log4net.
I tried my best to explain this example with pictures to begginers.
Please share your Feedbacks/Comments.

History

08/04/2017 - published Artical

UpComing Topic
How to write log in database using log4Net.

Comments