views:

36

answers:

1

I am using log4net code found at: http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html

How do I write the code behind to insert the information in to the table?

+2  A: 

MVC has no codebehind... you put it in the controller. First put the log4net configuration to use that appender in the config file (they give the appender config and SQL table info on that page you gave). Now in your controller you need a logger private in your controller:

private log4net.ILog log;

and now in the ctor of the controller (if it does not have one create it) you need to initialize the logger.

 log4net.Config.XmlConfigurator.Configure();
        log = log4net.LogManager.GetLogger(this.GetType());

now in your action you can log away...

log.Error("I lost my wookie");

or

try
{
  int x = 0/3;
  log.Info("The divide by zero didnt fail?  Why?");
}
catch(Exception ex)
{
   log.Error(ex);
}
CrazyDart
Thanks for that how do I need to create something like this? passing 4 parameters?? time,message,stacktrace...etc? but here we are just doing log.Error("I lostmy wookie"); what are the things it will add? how we are saying to add message? thread? leavel? date? etc...thanks
kumar
log.Error and the others on log. (like log.Debug) have overloads with message, and exception. The time, thread, stack trace and other things are automatically logged. The level is determined by the method you use... Error, Debug, Info, etc.
CrazyDart
BTW, I know its out of scope here, but the ADO appender has been updated to use a connection string from the connection strings section of the config, however the update is in the development version of log4net. I usually just take that appender out of the new code and add it to my project so I can use the old version of log4net because so many other libs use the old log4net. Thats a bit advanced for you now, but someday you will think of it and be able to find it now.
CrazyDart