How to Use log4net in ASP.NET MVC

by Virender
February 26, 2015


Download code – https://github.com/codeandyou/Log4Net

Step 1 – Add log4net “log4net.dll” library into your project.

Step 2 – Add Log4net setting in web.config

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<!-- Log4net section -->
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
<!-- Log4net section -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

Step 3 –  Add Log4net settings in Global.asax

1
log4net.Config.XmlConfigurator.Configure();
log4net.Config.XmlConfigurator.Configure();

Step 4 – Log4net settings

01
02
03
04
05
06
07
08
09
10
11
12
13
private static log4net.ILog Log { get; set; }
ILog log = log4net.LogManager.GetLogger(typeof(classtype));      //type of class
            log.Debug("Debug message");
            log.Warn("Warn message");
            log.Error("Error message");
            log.Fatal("Fatal message");
private static log4net.ILog Log { get; set; }


ILog log = log4net.LogManager.GetLogger(typeof(classtype)); //type of class


log.Debug("Debug message");

log.Warn("Warn message");

log.Error("Error message");

log.Fatal("Fatal message");

Step 5 – Run your project. And check that in c: drive log.txt file created and log entry done like this.

Note: After done every step if you not able to see any log in c: drive. That means your application don’t have administrator permission.

You can see such type of message – “ErrorCode: GenericFailure. Unable to acquire lock on file C:log.txt. Access to the path ‘C:log.txt’ is denied.”

Now start your visual studio as administrator or create log.txt file into c: drive and give him administrator persmission.

Share:

Tags:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related posts