Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Create Error Log files in Asp.net using C#, VB.NET with Example

Aug 5, 2015
Introduction

Here I will explain how to create error log files in
asp.net using c#, vb.net with example or code to create simple log files in asp.net using c#, vb.net. To generate log files in asp.net we need to get that errors in catch block and store it in folder or local drive in txt format or other format in asp.net using c#, vb.net.

Description:
  
In previous articles I explained bind all countries to dropdownlist in asp.net, generate random string or password in asp.net, Add or remove new row in gridview in asp.net,
bind dropdownlist in gridview in asp.net, gridview examples in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to generate error log files in asp.net using c#, vb.net with example.

To create error log files in asp.net first create one new folder "logfiles" in your application then  open your aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create Log Files in Asp.net using c#, vb.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnLog" runat="server" Text="Generate Log" onclick="btnLog_Click" />
</div>
</form>
</body>
</html>

Now add following namespaces in code behind

C# Code


using System;
using System.Web;
using System.Data.SqlClient;
using System.IO;
using System.Data;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLog_Click(object sender, EventArgs e)
{
try {
DataTable dt=new DataTable();
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
SqlCommand cmd = new SqlCommand("crudoperations", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@status", "SELECT");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
catch(Exception ex){
logerrors(ex.Message);
lblresult.Text = "Error occured please check complete details in log file";
}
}
public void logerrors(string error)
{
string pageName = Path.GetFileName(Request.Path);
string filename = "Log_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt";
string filepath = Server.MapPath("~/logfiles/" + filename);
if (File.Exists(filepath))
{
using (StreamWriter stwriter = new StreamWriter(filepath, true))
{
stwriter.WriteLine("-------------------START-------------" + DateTime.Now);
stwriter.WriteLine("Page :" + pageName);
stwriter.WriteLine(error);
stwriter.WriteLine("-------------------END-------------" + DateTime.Now);
}
}
else
{
StreamWriter stwriter = File.CreateText(filepath);
stwriter.WriteLine("-------------------START-------------" + DateTime.Now);
stwriter.WriteLine("Page :" + pageName);
stwriter.WriteLine(error);
stwriter.WriteLine("-------------------END-------------" + DateTime.Now);
stwriter.Close();
}
}

VB.NET Code


Imports System.Web
Imports System.Data.SqlClient
Imports System.IO
Imports System.Data

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnLog_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim dt As New DataTable()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Dim cmd As New SqlCommand("crudoperations", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@status", "SELECT")
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
Catch ex As Exception
logerrors(ex.Message)
lblresult.Text = "Error occured please check complete details in log file"
End Try
End Sub
Public Sub logerrors(ByVal [error] As String)
Dim pageName As String = Path.GetFileName(Request.Path)
Dim filename As String = "Log_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt"
Dim filepath As String = Server.MapPath(Convert.ToString("~/logfiles/") & filename)
If File.Exists(filepath) Then
Using stwriter As New StreamWriter(filepath, True)
stwriter.WriteLine("-------------------START-------------" + DateTime.Now)
stwriter.WriteLine(Convert.ToString("Page :") & pageName)
stwriter.WriteLine([error])
stwriter.WriteLine("-------------------END-------------" + DateTime.Now)
End Using
Else
Dim stwriter As StreamWriter = File.CreateText(filepath)
stwriter.WriteLine("-------------------START-------------" + DateTime.Now)
stwriter.WriteLine(Convert.ToString("Page :") & pageName)
stwriter.WriteLine([error])
stwriter.WriteLine("-------------------END-------------" + DateTime.Now)
stwriter.Close()
End If
End Sub
End Class

Demo

Create Error Log files in Asp.net using C#, VB.NET with Example

Now open your log file in "logfiles" folder that would be like as following 

Generated Error Log files in Asp.net using C#, VB.NET with Example

Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

ashish kachhi said...

plz enable download....

Suresh Dasari said...

Thanks ashish enabled download link

MohanJS said...

StreamWriter stwriter = File.CreateText(filepath);
its throwing error while creating text file
Can u pls tell me y?

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.