views:

54

answers:

1

Hi Experts,

I want to use Log4Net in my asp.net application & I have done the following things:

In Web.Config File

<configSections>
  <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections> 
<log4net>
  <logger name="File">
    <level value="All" />
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="Logs\\Log4Net.log"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>
  </logger>
</log4net>

In Global.asax

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    log4net.Config.XmlConfigurator.Configure();

}

and on Page_load

try
{
    // There is no such Session, this is just to create error
    if (Session["userName"].ToString() == "Admin")
    {

    }
}
catch(Exception ex)
{
     log4net.ILog logger = log4net.LogManager.GetLogger("File");
}

I have added the reference too in my BIN folder.

When I run my code, I got the error as per the expectation and after that new folder is created in my solution explorer with Log File. But the log file is empty.

I have no guess that why it happened, is it not able to track or record the error??

Please help. Thanks in advance.

+1  A: 

As leppie pointed out, there seems not to be any code that writes a log message. Your excpetion handler should propably look like this:

catch(Exception ex)
{
    log4net.ILog logger = log4net.LogManager.GetLogger("File");
    logger.Error("something went wrong", ex);
}

The configuration you posted also seems incomplete. You need a log4net section (cf. example configurations).

Stefan Egli
Thanks !! As I can see some more property of logger(object), I guess now it's time for me to play with Log4Net. Thanks again for your reply.
Rahul
It's almost 12 minutes that I have post my answer and I am trying to accept your answer but I am getting message "can not accept answer within 3 minutes". I think I have to bit a more.
Rahul
:-) Maybe this is interesting for you then: http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx
Stefan Egli
Can you please guide me how can I transfer it to MS SQL Server ?Can you redirect me to any good tutorial for it, because I am not able to track such.
Rahul