Solved

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

Posted on 2004-04-14
6
306 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
Comment Utility
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
Comment Utility
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
Comment Utility
in this case write logEntry to the Database

let me know if you need to know how
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:jayrod
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now