Quick and Dirty Guide to Configuring Log4Net For Web Applications

log4net logging aspnet 0 comments suggest edit

UPDATE: I wrote a post with notes on getting this to work with ASP.NET 2.0.

Looking around, I noticed a lot of people struggling with getting Log4Net to work with their web applications (ASP.NET 1.1). I’m not going to spend a lot of time digging into Log4Net here, as you can do a Google search for that. But I will give you a quick and dirty guide to quickly getting it set up for a website. Bar of soap not included.

Using a Separate Config File

Although you can put your Log4Net configuration settings within the web.config file, I prefer to use a separate configuration file. Log4Net is a bit of an elitist. It won’t dare put a FileSystemWatcher on web.config nor App.config. However, if you tell it to use its own config file, it will gladly monitor that log file and update its settings on the fly when the file changes.

Specifying the Log4Net Config File

If you use a separate config file, a quick and easy (and dirty) way to have your application find it is to place the config file in the webroot and add the following attribute to your AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator( 
ConfigFile="Log4Net.config",Watch=true )]

Declaring the Logger {.clear}

At the top of each class that I plan to use logging in, I declare a logger like so:

private static readonly ILog Log = LogManager.GetLogger( 
MethodBase.GetCurrentMethod().DeclaringType);

The reason I place a logger in each class is to scope it to that class. If you read the log4Net docs, you’ll see what I mean by this.

Using the Logger

Once you’ve declared the logger, you can call one its logging methods. Each method is named for the logging level. For example:

Log.Debug("This is a DEBUG level message.  
Typically your most VERBOSE level.");

Now whether that message shows up in your logs depends on how you’ve configured your appenders and the logging level you’ve set. Don’t understand what that means? Read the Log4Net introduction.

Sample Web Solution

In order to make all this discussion very concrete, I’ve gone ahead and did all your homework for you by creating a simple ASP.NET 1.1 web solution (Log4NetSampleSolution.zip ) using Visual Studio.NET 2003. After unzipping this solution, you should be able to build it and then view the default.aspx web page. This page will log a few very interesting messages of varying levels to three appenders.

Of special note is the use of the RollingFileAppender as seen in this snippet.

<appender name="RollingLogFileAppender"    
        type="log4net.Appender.RollingFileAppender">

    <file value="..\Logs\\CurrentLog" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />

    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
        <acceptOnMatch value="true" />

        <levelMin value="INFO" />
        <levelMax value="FATAL" />
    </filter>

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern 
        value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>

</appender>

Note that the file value (with backslashes escaped) points to ..\Logs\CurrentLog. This specifies that Log4Net will log to a file in a directory named Logs parallel to the webroot. You need to give the ASPNET user write permission to this directory, which is why it is generally a good idea to leave it out of the webroot. Not to mention the potential for an IIS misconfiguration that allows the average Joe to snoop through your logs.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

51 responses

  1. Avatar for sam
    sam March 11th, 2005

    Exactly what I was trying to accomplish. Thanks.

  2. Avatar for bags
    bags March 16th, 2005

    Great info - thanks. This works for a website and the classes (Page) objects in the website. What about when the website uses different class libraries? How do I configure the class libraries so that they, too, will log to the same location as the website code does? Can I have the class libraries point to the log4net config file that is used by the website?



    Thanks in advance.

  3. Avatar for Haacked
    Haacked March 16th, 2005

    That's the beauty of it. If your classes are declaring a logger, they will log to wherever the executing application is configured to log to.



    If you think of your application as an assembly graph, you need to configure your assembly and all the rest pick up that setting.



    Typically your root assembly is your website or your EXE.



    Hope that helps.

  4. Avatar for coolguy
    coolguy April 1st, 2005

    Thanks a lot. You don't believe I have been looking for it since 41/2 hours, most of the examples out there: either they are difficult to configure or difficult to understand.

    Good work, keep it up

  5. Avatar for Haacked
    Haacked April 1st, 2005

    Glad it helped! Thanks for the compliment.

  6. Avatar for Rohit
    Rohit April 8th, 2005

    Hello,



    We are evaluating log4net for one of our customers.



    Our application consists of Asp.Net application which uses remoting.

    We have included log4net in both web application and remoting application.

    but the log4net logs the debug info only for the web application and not in=

    the remoting application.

    It doesn't even creat the log file in the remoting application directory.

    we have given necessary permissions for the folders.



    Help on the same is much appriciated. Thanks!



    Regards,

    Rohit



    The following is mentioned in web.config of remoting application

    <configSections>

    <section name=3D"log4net" type=3D"log4net.Config.Log4NetConfigurationSect=

    ionHandler,log4net" />

    </configSections>



    <log4net>

    <appender name=3D"FileAppender" type=3D"log4net.Appender.RollingFileAppen=

    der">

    <param name=3D"File" value=3D"Test.log" />

    <param name=3D"AppendToFile" value=3D"true" />

    <param name=3D"RollingStyle" value=3D"Date" />

    <param name=3D"MaxSizeRollBackups" value=3D"30" />

    <param name=3D"DatePattern" value=3D"yyyyMMdd" />

    <layout type=3D"log4net.Layout.PatternLayout">

    <param name=3D"ConversionPattern" value=3D"%d [%t] %-5p %c [%x] - %m%n"=

    />

    </layout>

    <filter type=3D"log4net.Filter.LevelRangeFilter">

    <levelMin value=3D"DEBUG" />

    <levelMax value=3D"WARN" />

    </filter>

    </appender>

    <appender name=3D"EventLogAppender" type=3D"log4net.Appender.EventLogAppe=

    nder">

    <applicationName value=3D"ApplicationName" />

    <layout type=3D"log4net.Layout.PatternLayout">

    <conversionPattern value=3D"%date [%thread] %-5level %logger [%ndc] - %=

    message%newline" />

    </layout>

    <filter type=3D"log4net.Filter.LevelRangeFilter">

    <levelMin value=3D"ERROR" />

    <levelMax value=3D"FATAL" />

    </filter>

    </appender>

    <root>

    <level value=3D"DEBUG" />

    <appender-ref ref=3D"FileAppender" />

    <appender-ref ref=3D"EventLogAppender" />

    </root>

    </log4net>



    Also in the AssemblyInfo file we have added this line

    [assembly: log4net.Config.DOMConfigurator(Watch=3Dtrue)]=

  7. Avatar for Haacked
    Haacked April 8th, 2005

    Rohit, which version of Log4Net are you using. The config sample you sent me uses a different syntax than what I used.

  8. Avatar for Rohit
    Rohit April 11th, 2005

    Thanks for the reply, we are using .Net framework version 1.1 and log4net version log4net-1.2.0-beta8. Please let me know where we are going wrong.

    I also noticed that log.IsDebugEnabled(also any other IsxxxEnabled) function always returns false. I tried with the same configuration with other test projects i created it works but it doesn't work with my actual application. [It doesn't work with the Remoting application but same configuration works with web application]

    Thanks again fro the reply...

  9. Avatar for Haacked
    Haacked April 13th, 2005

    I'm using version 1.2.0.30714 and the only thing I noticed is that your configuration file was very different from mine in how you declared the appenders.

  10. Avatar for AspNet
    AspNet April 21st, 2005

    Hi all,



    I need help to set the log4net to delete log files automatically after 10days.

  11. Avatar for Al
    Al May 16th, 2005

    Hi



    I'm simply trying to get NUnit to run some tests that log, with the Test project being the one that starts the application logging. In fact, ive seen this before and this is what im trying todo: im using a configuration class (that will utlimately read from the registry or db) that says where the configuration path should be, and that in turn tells to write to file, ill write out the TestFixture first and then the config file. I dont get any errors but neither do i get a file created or written to... thanks for any help!



    [TestFixture]

    public class TestLogging

    {



    static TestLogging()

    {

    string configFile = Configuration.LogConfigFilePath;

    log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(configFile));

    ILog log = LogManager.GetLogger(typeof(TestLogging));

    log.Info("Starting Logging");

    }



    [Test]

    public void Test()

    {

    ILog logger = LogManager.GetLogger("my logger");

    logger.Info("Logging the Test() method");

    }

    }











    <appender name="FileAppender" type="log4net.Appender.FileAppender">

    <file value="c:\logs\log-file.txt" />

    <appendToFile value="true" />

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

    <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

    </layout>

    </appender>

  12. Avatar for Emil
    Emil May 20th, 2005

    Great, just what I needed. Been pounding my head against the wall for last couple of hours, trying to get it work, and this was excactly what I needed to get starting.



    I've made 2 slight chances though



    1. I put the assembly line in the global.asax instead of the assembly.cs. As far as I can tell it makes no difference and it just seems a bit more 'logical' place to me - purely a question of taste (and of where I suspect my collegues will be able to find it :)



    2. I've used

    [assembly: log4net.Config.XmlConfigurator( ConfigFile="Log4Net.config",Watch=true )]



    Although it compiled just fine, I got a warning saying that the DOMConfigurator was obsolete and that the XmlConfigurator should be used instead - so I did :)





    /emil@obey.your.compiler.or.die.com

  13. Avatar for Evan
    Evan June 21st, 2006

    Any idea why my IsXXXXXEnabled properties always return false? Here is my config...
    <log4net>
    <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <!--connectionTyte value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /-->
    <connectionString value="Data Source=EVAN-YPW0LVYKWZ;Initial Catalog=PICTest;Integrated Security=True" />
    <commandText value="spLogEntryAdd" />
    <parameter>
    <parameterName value="@dtTimeStamp" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
    <parameterName value="@szLogger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%logger" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@szLevel" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%level" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@szThread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%thread" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@szMessage" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@szException" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
    </appender>
    <root>
    <level value="DEBUG" />
    <appender-ref ref="AdoNetAppender_SqlServer" />
    </root>
    <logger name="RRR.LoggingManager">
    <level value="INFO" />
    </logger>
    </log4net>
    Thanks,
    Evan

  14. Avatar for Daniel
    Daniel June 25th, 2006

    Evan,
    It may be the config file is not being detected (assuming you use a separate log file, as Haaked illustrates above).
    Make sure you have an AssemblyInfo.cs file in your App_Code folder with the required [assembly: log4net.Config.XmlConfigurator...] bit adjusted for your application.
    Hope this helps...
    Daniel

  15. Avatar for Lin
    Lin July 6th, 2006

    I tried this sample code and set it up. But there is no log file created. Could someone here help me out?

  16. Avatar for rnet
    rnet July 9th, 2006

    I also tried this code but there is no log file created.

  17. Avatar for kode
    kode July 14th, 2006

    If no log file is created, check your Log4net.config. There are three appenders in that. If your SMTP server info is not correct in the config file, log file might not be created. I advise you to remove the first two appenders if you dont need them. Atleast thats what happened in my case.
    Hope it helps.

  18. Avatar for Ginil
    Ginil August 1st, 2006

    Hi,
    I am using VS2005, and in my web application, I do not have an AssemblyInfo.cs file. Is that bad?
    I have read that it can be put in the global.asax file, but where exactly in it? My gloabal.asax files pretty much contains session_start(), .._end(), application_start(), ..end().
    Also, is there an example log4net.config file I can use?
    I am new to .NET and ASP.
    I need to get log4net working in my web application.
    Kind Regards,
    Ginil.

  19. Avatar for rendomizer
    rendomizer September 10th, 2006

    your assemblyinfo.cs and global.asax.cs files are located in the app_code folder of your application.

  20. Avatar for you've been HAACKED
    you've been HAACKED September 27th, 2006

    Log4Net Troubleshooting

  21. Avatar for adam
    adam November 14th, 2006

    hi, your example was extremely useful on teh c# project i developed.
    do you have a VB.net example of these two lines
    [assembly: log4net.Config.XmlConfigurator(
    ConfigFile="Log4Net.config",Watch=true )]
    and
    private static readonly ILog Log = LogManager.GetLogger(
    MethodBase.GetCurrentMethod().DeclaringType);
    your examples are amazingly simple to implement and adapt. a vb translation would save me hours. thanks!!

  22. Avatar for Haacked
    Haacked November 14th, 2006

    The first is an assembly level Attribute declaration. I think the syntax is like this:
    <Assembly: log4net.Config.XmlConfigurator(
    ConfigFile="Log4Net.config",Watch=true )>
    Just look up "Attributes" and VB.NET.
    The second is probably something like:
    Private Shared ReadOnly Log As ILog = LogManager.GetLogger(
    MethodBase.GetCurrentMethod().DeclaringType)
    Hope that helps.

  23. Avatar for Kaundinya
    Kaundinya December 7th, 2006

    Log4Net is not working for ASP.NET 2.0 when we deployed the web site to the web server.We made sure all the permissions are all proper.
    Can anybody help me.It works like a charm local machine but not on the Web Server.
    Please Advise...
    Kaundinya

  24. Avatar for Atiz
    Atiz March 6th, 2007

    Hi all,
    when I try to use
    [assembly: log4net.Config.XmlConfigurator(
    ConfigFile="Log4Net.config",Watch=true )]
    in the assemblyinfo.cs, the compiler complained that it is not an attribute class. How can I make it work? I am using VS2005 to do my C#.net proj. I've already referenced to log4net.dll.
    TIA
    Atiz

  25. Avatar for PHANI NADIGADDA
    PHANI NADIGADDA April 30th, 2007

    Log4Net for logging the messages is working fine for me. The thing is my sessions are getting expired when the I log the message using
    log.Debug() or log.Info() or log.Error() or any thing.
    Does any know how to overcome this. If, Please help me to get out this issue.
    Thanks,
    Phani
    phani.nadigadda@gmail.com

  26. Avatar for yoi
    yoi May 16th, 2007

    hallo

  27. Avatar for Imtiyaz
    Imtiyaz July 5th, 2007

    I have a ASP.NET 2.0 project in which i have used Log4net 1.2.9.0 configured it through Log4Net.config Files, i have tried to debug, it works fine if i configured my project to use "ASP.NET Development Server", but it did not log any thing if i configured my project to use "IIS Web Server".
    In Application_Start event of Global.ASAX
    i had put below code.
    log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath(Exact Path)));

  28. Avatar for Hemanth
    Hemanth August 28th, 2007

    We used Log4net.It was working fine for a long time.But suddenly it stopped writing the log file.can you let me know what may be the cause.
    Also,we are using it for two applications.The same folder is configured for the log file.While the log is being created for one folder,the same is not created for the other application

  29. Avatar for Scott
    Scott December 2nd, 2007

    Make sure that you add the assemblyinfo.cs reference to every project you are emitting debug output from. If you don't you will only get the debug info from the dlls that have this compiled in.

  30. Avatar for Miller
    Miller January 17th, 2008

    Hello,
    I made this with c#2005 and .net2 on log file n sql2005
    now i want to create different log files per day.
    thanks in advance

  31. Avatar for Miller
    Miller January 17th, 2008

    here is my code. creates daily logs.. u can custimize it as hourly too. log file name : yyyyMMdd.TXT
    use capital letter which are not defined in class.

    <root>
    <!-- Log Level -->
    <level value="ALL" />
    <!-- Targets -->
    <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <!-- Log path -->
    <param name="File" value="logs\\" />
    <param name="AppendToFile" value="true" />
    <!-- Log filename -->
    <datePattern value="dd-MM-yyyy.TXT" />
    <maxSizeRollBackups value="10" />
    <rollingStyle value="Date" />
    <maximumFileSize value="1MB" />
    <!-- set to false to create daily or hourly logs -->
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %u %c %l %m %n" />
    </layout>
    </appender>

  32. Avatar for Sameer Alibhai
    Sameer Alibhai February 6th, 2008

    Here guys, this will explain to you how to set up the configuration in your global.asax or web.config
    http://webonweboff.com/tips...

  33. Avatar for m
    m May 1st, 2008

    I've been trying to get log4net to work with asp.net 2.0 and a database table. Here is the config file:
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <!--<add key="Default" value="Database=CJISMNIDb;Server=CJISDEVSQL01;Integrated Security=SSPI;"/>-->
    <connectionString value="server=CJISDEVSQL01;database=EventLog;integrated security=true;" />
    <commandText value="exec LD80Event_CREATE @eventDate, @logLevel, @message, @exception, @updateUserName" />
    <parameter>
    <parameterName value="@eventDate" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
    <parameterName value="@logLevel" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%p" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%m" />
    </layout>
    </parameter>
    <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
    <parameter>
    <parameterName value="@updateUserName" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%p" />
    </layout>
    </parameter>
    </appender>
    <logger name="eventLog">
    <level value="ALL" />
    <appender-ref ref="ADONetAppender" />
    </logger>
    <root>
    <level value="ALL" />
    <appender-ref ref="ADONetAppender" />
    </root>
    </log4net>
    This is the line of code in Global.asax, Application_Start method:
    log4net.Config.XmlConfigurator.Configure();
    This is in the default page:
    private static readonly ILog eventLog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    The issue is all the properties are 'always' set to false when the application is running and nothing is ever written to the database. I have been searching the web for 2 whole days now and I can not find any information that has made a difference.
    Can you help with this problem?
    Thank you.

  34. Avatar for Joe
    Joe May 6th, 2008

    Thanks. That was quick and easy.

  35. Avatar for siva
    siva May 14th, 2008

    Can anybody tell me how to delete the old log files using log4net. I know using the property "maxsizerollbackups" which is doing the roll back of a file when it reaches to max size.i am using the rolling style as "Date". So every day log file is created but i want to set an option of delete the old files for last 2week or 3 weeks or 10 days like that...
    Your help is greatly appreciated..
    Thanks
    Siva

  36. Avatar for Abel
    Abel August 17th, 2008

    Hi Phil,
    In Visual Studio 2005 and 2008 you can create a Web Site (NOT meaning a Web Application!). A "Web Site" does not have an assemblyinfo.cs or assemplyinfo.vb. Apparently, somehow these assembly-related lines have to be added to web.config (unser system.web?) but I don't know how. I think it would make your explanation more worthwhile if you include these two situations (Web Site in VS2005/2008) and how to configure them in your text...
    (Many thanks to Microsoft for deciding on having these subtle differences in configuration and compiling between Web Applications and Web Sites of which many people seem to be unaware, resulting in a lot of hair loss ;).
    -- Abel --

  37. Avatar for Omar
    Omar August 26th, 2008

    Hi,
    i have just started working on the Log4net and found this discussion very helpful. i am working on a application which is using ASP and .net components. We are in middle of transition from ASP to asp.net but want to use Log4Net with Asp pages.
    Is that possible and how to configure log4net for this. Any help on this would be great.

  38. Avatar for Sean
    Sean September 4th, 2008

    For me on .net 2.0, using the Assembly annotation instead of the Global.asax.cs method made it work.
    My configuration is in the web.config.
    Also, for debugging, if you look at the ILog object, if all the IsDebugEnabled, IsErrorEnabled, et al, are FALSE, then you aren't loading the configuration file. Hope that helps the next person.

  39. Avatar for Peter
    Peter September 30th, 2008

    I have a WebService (.NET 2) which is using log4net and it stops logging after a while for no obvious reason. I have used log4net on a WebService under .NET 1 which works perfectly. Needless to say I have tried all the suggestions I'm using version 1.2.10.

  40. Avatar for Kapil
    Kapil November 5th, 2008

    I am following exact steps listed in the tutorial above but i am not getting log file generated untill i am granting write permission to <machinename>\USERS in my case (KAPIL\USERS)please note ErrorLog.txt is already having suitable write permission for ASPNET account.
    Please let me know why i need to grant permission to MACHINE\USER which not logical and vulnerable too.

  41. Avatar for Peter G
    Peter G November 20th, 2008

    This is Great Advice when running in development environments. After a day of trying to figure this out with various posts, I did this and it worked immediately. I'm assuming though when it's running in IIS, you'll have to specify a fully qualified path for the log file, because the root would be in you windows directory, also you can probably set up a web folder in the MSI that does the installation and grant the appropriate privileges in the installer. Haven't tried this yet, but I think that's probably how it'll work.

  42. Avatar for Jess H
    Jess H December 9th, 2008

    Hello,
    I have followed the steps above and my code appears to be logging but no longer file is being generated. Can anyone help?
    Thanks

  43. Avatar for Hung
    Hung December 9th, 2008

    I want to log MySQL. Help me

  44. Avatar for sandy
    sandy January 12th, 2009

    hello,
    i wanted to know whether log4net supports logging in the database apart from the specified columns like message,logdate,loglevel,thread,logger.
    ex: i want to add a extra column as userID . is it possible ? and if yes how to do it.
    please let me know.
    thanks.

  45. Avatar for John Halliday
    John Halliday August 24th, 2010

    I have configured a simple VS2008 webapp to use the log4net ADONetAppender and get no errors. Yet, I also get no records inserted into the database. I have ensure that the bufferSize is either 0 or 1 (tried both ways) and also ensured that the IsDebugEnabled and IsErrorEnabled are enabled (both are true at
    runtime). Why can't I get records into the DB?
    I have placed the following static variable at the top of my page class:
    private static readonly ILog logger = LogManager.GetLogger(typeof(Logger));
    and I use the following simple function:
    protected void Button1_Click(object sender, EventArgs e)
    {
    //log4net.Config.DOMConfigurator.Configure();
    log4net.Config.XmlConfigurator.Configure();
    logger.Debug("Debug");
    logger.Debug("The expected logic failed validation");
    }
    My web.config code:
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    </configSections>
    <log4net debug="true">
    <root>
    <level value="DEBUG" />
    <appender-ref ref="ADONetAppender" />
    </root>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="0" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, requirePermission=false" />
    <connectionString value="server=OURSERVERNAME; uid=DVUSER; pwd=123abc; database=DevCadence" />
    <commandText value="INSERT INTO JohnsLog ([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.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    </parameter>
    <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%thread" />
    </parameter>
    <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout" value="%level" />
    </parameter>
    <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%logger" />
    </parameter>
    <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout" value="%message" />
    </parameter>
    </appender>
    </log4net>
    Thanks for any and all help.

  46. Avatar for Bev
    Bev June 28th, 2011

    Hi Phil
    Unzipping the sample application throws an error. Is there any other place to get the sample app?
    Thanks

  47. Avatar for Zeeshan Umar
    Zeeshan Umar June 28th, 2011

    Nice article, it really helped me in configuring Log4Net. Thanks for sharing.

  48. Avatar for kittu
    kittu September 18th, 2011

    I am using in my application with front end ASP pages and back end with transactions for Java and i am facing any issue while the application is running fine once in a while the application is hand and i am unable to check the what exaclty the issue, and i want to add some log to the my web site to Virtual directory what exaclty the issue
    and when i restard the services application is running fine.
    and i am new to the asp, pls help on this.
    thanks in advance

  49. Avatar for Nissim Levy
    Nissim Levy March 13th, 2012

    I have two appenders in my app.config for a .net C# application. One is for a database log and the other for a rolling file appender. I need to use these two appenders but with a different message parameter to each one. I therefore cannot just use one log.info call but I need to instantiate two ILog objects, each one connected to a different appender from my app.config. Is this possible?

    Thanks for any help you can offer.

  50. Avatar for tasoss
    tasoss May 15th, 2014

    Hello.I know it's an old post but i would like to mention that the zip is damaged.Could you please replace it?Thanks!

  51. Avatar for Warren LaFrance
    Warren LaFrance April 22nd, 2016

    Seems your zip file is bad now..