How to store log in database using log4net

Hi Dear,
I have moved my content to my own hosting. To see the existing content please go the following url

From now you will get blog post regularly. To see my new blog site please Click here. Feel free to leave comments I will try to reach you as soon as I can. Hope you will enjoy all the article.

31 thoughts on “How to store log in database using log4net

  1. Just want to mention that Log4Net has the possibility to reload configuration dynamically when the config is changed, typical used if you change the log level when the application is running.

    To activate this you need to use the XmlConfigurator.ConfigureAndWatch instead of XmlConfigurator.Configure()

    The ConfigureAndWatch can be invoked using several methods my personal favorite is using an assembly-level attribute:
    [assembly: log4net.Config.XmlConfigurator(Watch=true)]

    Best regards
    Georg Jansen – Log analysis and monitoring made easy

  2. Hi friend,

    I used Log4net as above said.It is very good.
    But i faced one problem is data which we entered in to database is not commited immediatly.after closing app/dadaserver it is coming.
    plase help on this waiting for your reply.

    Thanks regards

  3. Hello,

    I’m facing with the same problem.. data which we entered in to database is not commited immediatly.after closing app/dadaserver it is coming.
    Why happens this? How can data be commited without shutting down the server? I’m using C#, not VB.


  4. Great article. How do i get the identity of the log i have just inserted.

    For eg. i want to display the id to user so that they can contact support team in case of exception.

    So once i call Log.Fatal it should return me the id. Can i override Fatal method?

  5. Hi, I`m trying to store my logs in my database(SQL Server 2005), I tried to use this code but nothing happened, Do I need to do anything else??
    Maybe write another code in another class…

  6. Hi,

    I am using sqlserver2008 and i hav done all the steps mentioned above but the log is not updating in the database i am not getting any error msg

  7. Also I have another question. I have a project. There are three types of users.Users,Administrator etc. I want to separete their own log. I created UserLogTable and AdminLog also I want to also create an order table which is keep orderLog. How can I do this?

    • Hi, i set buffersize value r2 =1 but no luck i configure exactly that is explained before any ideas? using sql server

  8. To get this working in the App.Config you need to correct a small error which is to correct the case as shown below. Note that Log4net has an upper case L after Log4NetConfigurationSectionHandler,Log4net when it appears that it should be a lower case l.
    INCORRECT (Note upper case L after the comma, change this to a lower case) :

    CORRECT (lower case l after the comma, this work for me.):

  9. You need run this line of code: log4net.Config.XmlConfigurator.Configure(); in Application_Start of your Global.asax when using with It’s true in my case, I have waste long time because log4net doesn’t running until I add this code.

  10. 1. Change all relevant references in web.config to [case sensitive] “AdoNetAppender” and 2. set bufferSize value=”1″ if you want immediate update to database while testing this. Thanks for this blog.

  11. Hey guys,
    For thos of you having a hard time trying to make this work, in my case, I found my solution issuing this within VS.NET immediate window:


    It thrown the Sql exception below. I fixed the null issue and it worked like a charm !

    log4net:ERROR [AdoNetAppender] Exception while writing to database
    System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column ‘event_type_id’, table ‘reduxssp_test.dbo.eventlog’; column does not allow nulls. INSERT fails.
    The statement has been terminated.


    Sylvain Audet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s