Background
Logging the errors of a live application is very important to avoid any inconvenience for the users. In C# to handle exceptions the try, catch and finally keywords are used, so in this article, we will learn how to catch the error and send the error details by Email so developers can fix it as soon as possible.
So let us start by creating the application.
Use the following procedure to create a web site:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New" - "Web Site..." then select "C#" - ""ASP.NET Empty Web Site"" (to avoid adding a master page).
- Provide the project a name such as "ExceptionLoggingToEmail" or another as you wish and specify the location.
- Then right-click on Solution Explorer then select "Add New Item" - "Default.aspx" page.
- Drag and Drop one GridView to the Default.aspx page. Then the page will look as follows.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body bgcolor="silver">
- <form id="form1" runat="server">
- <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- </asp:GridView>
- </form>
- </body>
- </html>
In the preceding source code, we have taken the one grid view and we try to assign a file as the data source for the grid view that is not available so it can generate the error file not found and we can send these error details to an Email.
Now create the class named ExceptionLogging to log the error to Email then write the following code:
ExceptionLogging.cs

prince tomyPosted Jul 26, 2019, 6:45 AM
Nice explanations.Helpful explanations
Reddu RPosted May 2, 2018, 11:20 AM
Thanks a lot, I follow your tutorial correctly and its working. Thanks again
Jes SiePosted May 11, 2017, 5:49 AM
Hi, first I would like to thank you for sharing your code. I tried this but I have not receive any email. I follow your tutorial correctly. Could you guide me on how to do this?