Error Logging in VB and C#

Alright, so lately I have been working on a couple of ASP.NET(C#) projects.  One thing I have seen is nothing is as useful as having a good error logger.  Now, since I am still in the learning phases of .NET programming, I have been looking to a coworker, Joe Young, sort of as a mentor.  He provided me with some code that has just immensely helped me.  I have attached 2 files.  one in VB, on in C#, they are Joe’s code.  These are your App_Code files.  They will create a detailed log of any errors in a file in a directory you specify.  You specify the file in the attached files.  To use them, you will insert the following code. (For the below VB examples, I used a website to convert it from my C#, as I was being lazy)

C#:

private void LogError(Exception errMessage)
{
errorlogger objLog = new errorlogger();
objLog.logerror(errMessage);
}

VB:

Private Sub LogError(ByVal errMessage As Exception)
Dim objLog As New errorlogger()
objLog.logerror(errMessage)
End Sub

This allows you to call the external errorlogger.

Now, to call it. The most useful place I have put these are in SQL connections/procedure calls. Here is an example from one of my programs.

C#

try
{
string strSQLSetCheckedOut = “exec CP_setCheckedOutTrue ” + HttpContext.Current.Session["sName"].ToString();
SqlCommand conSQLCommand = new SqlCommand(strSQLSetCheckedOut, conSQLConnection);
conSQLCommand.CommandTimeout = 120;
conSQLConnection.Open();
conSQLCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
LogError(ex);
}
finally
{
conSQLConnection.Close();
}

VB:

Try
Dim strSQLSetCheckedOut As String = “exec CP_setCheckedOutTrue ” & HttpContext.Current.Session(”sName”).ToString()
Dim conSQLCommand As New SqlCommand(strSQLSetCheckedOut, conSQLConnection)
conSQLCommand.CommandTimeout = 120
conSQLConnection.Open()
conSQLCommand.ExecuteNonQuery()
Catch ex As Exception
LogError(ex)
Finally
conSQLConnection.Close()
End Try

Hopefully this little bit of code out there will help someone else out.

C# ErrorLogger Code

VB ErrorLogger Code

Share and Enjoy:
  • email
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Ping.fm
  • LinkedIn
  • Facebook
  • Twitter
  • Google Bookmarks
  • Live
  • Blogplay
  • Add to favorites
  • blogmarks
  • Mixx
  • Blogosphere News
  • Diggita
  • Diigo
  • DotNetKicks
  • eKudos
  • Fark
  • Faves
  • FriendFeed
  • Gwar
  • HackerNews
  • Internetmedia
  • laaik.it
  • LinkaGoGo
  • LinkArena
  • Linkter
  • MisterWong
  • MSN Reporter
  • MyShare
  • MySpace
  • Netvibes
  • NewsVine
  • Posterous
  • RSS
  • Slashdot
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Wikio IT
  • Yahoo! Bookmarks
  • Yahoo! Buzz

Tags: , , , , , , , , ,

Leave a Reply