Solved

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

Posted on 2004-04-14
6
310 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 250 total points
ID: 10825923
in this case write logEntry to the Database

let me know if you need to know how
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

825 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