Usually we are using different methods to log errors in our application. Log4net is one of the best tool to log in the database/ file/email etc. Here explain how to create log in database and file.
At the first, download the MySQL-Connector from http://dev.mysql.com/downloads/connector/net/
And download log4net from http://logging.apache.org/log4net/download.html
Create table in mysql database for storing log
CREATE TABLE `log` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime NOT NULL,
`Thread` varchar(255) NOT NULL,
`Level` varchar(50) NOT NULL,
`Logger` varchar(255) NOT NULL,
`Message` varchar(4000) NOT NULL,
`Exception` varchar(2000) DEFAULT NULL,
PRIMARY KEY (`Id`)
Then start a new project in Visual studio and add reference to the above dlls to the project.
After this, add configuration for log4net in the web.config file
Please make sure that the version and publickey of the mysql.data.dll in the config file is same as that the downloaded dll.
Then, add below code in the global.asax
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
And this is a simple log in the aspx page:
public partial class _Default : System.Web.UI.Page
{
private static readonly ILog log = LogManager.GetLogger(“UI Layer”);
protected void Page_Load(object sender, EventArgs e)
{
log.Info(“Test Log Message”);
}
}
In case of any issue the log is not created in table. We can enable internal debug of the log4net to find the issue. For this add below configuration in the web.config.
Then if we check the file d:\tmp\log4net.txt to find out the issue.
Would be nice if you could also include the necessary configuration.