Send an email whenever error occurs in your code using log4net
Posted By : Yash Arora | 21-Nov-2018
In this blog, I will explain the configuration of log4net in .net project So that whenever an Error occurs in your code it will send a mail.
So what is Log4net?
Log4net is a logging framework which helps to log your application. Logging is very useful to keep track of the flow of the application and debug the application.
With the help of log4net configuration, we can write our .NET logs to a file on disk, a database or potentially dozens of other places, all without changing our code.
What was the need to implement this?
In IT industry sometimes comes up with a situation in which some error occured on client side and before client makes any complaint on this we try to fetch what was the error that occured on client machine. So one of the ways is to get mail on whatever error occured.
Prerequisites:-
1.Microsoft visual studio.
2.Windows operating system.
Let's start with the import of the Log4net package
1. You can download log4net.dll from NuGet-package manager in Microsoft visual studio
Now add log4net configuration in App.config file
Here in this App.config file I have used different levels of logging like INFO,ERROR,DEBUG.
You can use different appender for all levels of log.
Like for error log it will send log in file as well as in email.
In type you can specify which kind of appender you want to use like log4net.Appender.SmtpAppender
In layout you can specify in which format data will come in file or in mail.
So till now, we have added configuarion for log4net.
Let's create a cs file and see how to use it.
using log4net;
namespace Abc
{
static class Program
{
static readonly ILog Log = LogManager.GetLogger(typeof(Program));
static void Main()
{
try{
FileInfo configFile = new FileInfo(App.config);
XmlConfigurator.ConfigureAndWatch(configFile);
Log.Info("Application Started");
int x=5/0;
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SalesConnectorService()
};
ServiceBase.Run(ServicesToRun);
}catch(Exception e){
LogManager.getLogger("errorlog").Error(e.Message);
}
}
}
}
Once the program will run With the help of xmlConfigurator it will look for log4net configuration from App.config file and create a log file on the path that you have specified in config file.
In the catch blog I have mentioned LogManager to specify which logger I want to use that I have mentioned in App.config file.
Reference:-
For different log4net configuration like different pattern of mail data.You can refer here.
Hope it will be helpful for you.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Yash Arora