?
Solved

Custom Exception Publisher class to publish to database using Data Access application block

Posted on 2004-04-14
6
Medium Priority
?
320 Views
Last Modified: 2012-06-21
I'm looking for examples of custom publishers and some walk throughs on the best way to create a custom publisher to keep track of multiple websites exceptions.
Some create table scripts would be nice as well..

Thanks for any input
0
Comment
Question by:jayrod
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10825081
you cancreatan error log and write to it

' Writes an entry to an existing log
Public Sub WriteLogEntry( _
     ByVal logName As String, _
     ByVal logSource As String, _
     ByVal logEntry As String, _
     ByVal logType As System.Diagnostics.EventLogEntryType, _
     ByVal logID As Integer, _
     ByVal category As Short)
    'Arguments : logName - The name of the log
    '            logSource - The source that writes the log
    '            logEntry - The text of the new message to log
    '            logType - The type of log to enter (error, failure
    '                       audit, information, success audit, warning)
    '            logID - An integer that sets the entry's ID
    '            category - The category for the entry

    Dim myLog As New System.Diagnostics.EventLog()
    myLog.Log = logName

    ' Create the source, if it does not already exist.
    If Not System.Diagnostics.EventLog.SourceExists(logSource) Then
        System.Diagnostics.EventLog.CreateEventSource(logSource, logName)
    End If

    myLog.WriteEntry(logSource, logEntry, logType, logID, category)

End Sub
0
 
LVL 3

Author Comment

by:jayrod
ID: 10825131
I use the error log now.. I'm looking for a database solution.

Thanks though
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 750 total points
ID: 10825923
in this case write logEntry to the Database

let me know if you need to know how
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 3

Author Comment

by:jayrod
ID: 10826007
what i'm looking for is maybe an example table schema that someone would use to hold the exceptions (i know it's a sql thing but I thought the folks over in sql server wouldn't know what I meant)

and also how you use your new object?
0
 
LVL 20

Assisted Solution

by:ihenry
ihenry earned 750 total points
ID: 10937168
What if you have a .NET tool you can use in your custom publisher to output log messages to a variety of targets like windows log, database, xml or flat file, windows messenger, smtp and many more. Features with hierarchy logger instances, level of messages priority and FREE!

try this:
Log4Net, http://log4net.sourceforge.net/old-index.html

Now back to the question :)
When I need to log an error message, usually I need a few information like
1. Date and time when the error occurs
2. Machine and Domain name, it's useful for web app host in multiple web server
3. Current app user ( Thread.CurrentPrincipal.Identity.Name, useful for form based authentication or anything except windows authentication )
4. Current logged windows user ( WindowsIdentity.Name )
5. Error message description, off course
6. Error number, it happens sometimes you don't want the real message revealed for security reason and your custom exception will throw the error number description instead.
7. Anything else?
0
 
LVL 3

Author Comment

by:jayrod
ID: 10939190
This is the code I ended up using:

            void IExceptionPublisher.Publish(Exception exception, NameValueCollection AdditionalInfo, NameValueCollection ConfigSettings)
            {
                  SqlConnection connection = null;

                  // Create StringBuilder to maintain publishing information.
                  StringBuilder strInfo = new StringBuilder();
                  
                  // Record the contents of the AdditionalInfo collection.
                  if (AdditionalInfo != null)
                  {
                        // Record General information.
                        strInfo.AppendFormat("{0}General Information{0}", Environment.NewLine);
                        strInfo.AppendFormat("{0}Additonal Info:", Environment.NewLine);
                        foreach (string i in AdditionalInfo)
                        {
                              strInfo.AppendFormat("{0}{1}: {2}", Environment.NewLine, i, AdditionalInfo.Get(i));
                        }
                  }
                  // Append the exception text
                  strInfo.AppendFormat("{0}{0}Exception Information{0}{1}", Environment.NewLine, exception.ToString());
                  try
                  {
                        connection = Helper.GetConnection();                              

                        SqlParameter[] parms = new SqlParameter[4];
                        parms[0] = new SqlParameter("@excMessage", exception.Message);
                        parms[1] = new SqlParameter("@ExcSource", exception.Source);
                        parms[2] = new SqlParameter("@ExcTargetSite", exception.TargetSite.Name);
                        parms[3] = new SqlParameter("@ExcAddInfo", strInfo.ToString());

                        SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "[NRLFCU_MASTER].[dbo].ExceptionInsert_sp", parms);
                  }
                  catch(Exception ex)
                  {
                        throw(ex);
                  }
                  finally
                  {
                        if(connection != null)
                              connection.Dispose();
                  }
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question