views:

15

answers:

1

Hi All,

I want to make Log4Net to enter logs in MS Sql Server 2005. I am using .NET 2.0. Here is the code till now I have written:

Web.Config File

<configSections>
  <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<connectionStrings/>
<log4net>
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=XYZ;Initial Catalog=ACC;Integrated Security=True" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@thread" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@exception" />
      <dbType value="String" />
      <size value="2000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>

</log4net>

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();

}

Page_load

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

    }
}
catch(Exception ex)
{
    log4net.ILog log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
    log4net.Config.XmlConfigurator.Configure();  
    log.Debug("log Debug",ex);  
    log.Info("log Info",ex);  
    log.Warn("log Warn",ex);  
    log.Error("log Error",ex);  
    log.Fatal("log Fatal",ex);           
}

Actually I have tried it by storing lod details in Log folder in my directory, which was working fine. But I want to store information in Sql Server 2005.

Am I missing something? or I have to correct some where.

Please help.

Thanks in advance

A: 

Unless I made some mistake when I edited your question, then the problem is that you do not reference the appender like this:

<root>
  <level value="ALL" />    
  <appender-ref ref="AdoNetAppender" />
</root>

Some further comments:

  • You do not need to call the Configure method twice
  • You do not need an exception to test logging
Stefan Egli
In general cases we use to put the login User Name or role into the database too. How can I do it? I have modified stored procedure, Now my table has one more column i.e. UserName. How can I provide userName to the database ?
Rahul
yes, after the appender. for the username you need to configure an additional parameter and do what is suggested here: http://stackoverflow.com/questions/3007612/track-user-with-log4net-for-a-website/3009077#3009077. but I really recommend that you read that tutorial (that I posted in your last question) first. It is important to understand a few key concepts before you use log4net. In the end you will save time if you read it before just trying out something.
Stefan Egli
Thanks for your help and valuable advice. I am now going to read some tutorials. Thanks again
Rahul