2

I would like to have log4net log to my database. I have added an AdoNetAppender as written in the docs . The docs also give the definition for the Log-table to create. Since I am using Entity Framework with Code First and I am recreating the database quite often (DropCreateDatabaseAlways) during development, I don't want to manually create the table. I tried to create a class Log instead that matches the definition in the docs.

public class Log
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string Thread { get; set; }
    public string Level { get; set; }
    public string Logger { get; set; }
    public string Message { get; set; }
    public string Exception { get; set; }
}

However, when using the AdoNetAppender I get an error that seems unrelated

Cannot drop database "aspnet-myDb-20150625044347" because it is currently in use.

but that disappears if I take out the AdoNetAppender.

How can I log to my database with Entity Framework code first? Thank you for any help.

peter
  • 2,103
  • 7
  • 25
  • 51
  • 1
    You could try issuing a call such as `context.Set();` before any initialization code of `log4net`. I'm suspecting the logging framework is connecting to the database before EF attempts to drop and recreate the database resulting in the drop operation failing. – Jurgen Camilleri Jul 14 '15 at 14:44
  • @JurgenCamilleri, this was exactly my issue, the Pooling wasn't resolving my case and when I commented out the Logger declaration at the top of my Controller (which strangely works in other older projects) Boom => Works! So I'm going to think how to implement everything for testing and production because calling the context.Set(); doesn't help ... THANK YOU! – Dimitri Nov 17 '19 at 15:16

1 Answers1

3

The answer here may solve your problem. The main issue is that the database still has an open connection.

short clip is to use Pooling=false. Being that this is in development you should not need pooling.

I know answers with links are discouraged but this is a link to another SO answer so I think we are safe here.

Hope this helps

Community
  • 1
  • 1
workabyte
  • 3,496
  • 2
  • 27
  • 35
  • `Being that this is in development you should not need pooling.` What will OP do when it's time for shipping? – Jurgen Camilleri Jul 14 '15 at 14:45
  • @JurgenCamilleri, when they ship it out it will not be a drop and create always (I_HOPE) so this open connection issue should not be an issue. And setting up a transform to alter the connection string to use pooling for the release solves for the change in settings. Hope I answer your question properly – workabyte Jul 14 '15 at 14:46
  • I think the issue would still persist but you may be right. – Jurgen Camilleri Jul 14 '15 at 14:51
  • That solved the problem, log4net now logs to the database. Thanks for asking my next question, Jurgen ;o). So I should remove pooling in release? (drop create won't be necessary there) – peter Jul 14 '15 at 14:56