Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

Synchronize Now!


Log Messages

  • It's simple to create detailed and informative logs with a minimal amount of code:


protected void Log(string Category, string Message)
{
    LogEntry logEntry = new LogEntry();
    logEntry.Categories.Add(Category);
    logEntry.Title    = Application.ProductName;
    logEntry.EventId  = ++eventId;
    logEntry.Priority = 0;
    logEntry.Message  = Message;
    logEntry.ExtendedProperties.Add("Protocol", ProtocolName());
    Logger.Write(logEntry);
}


  • No coding is necessary to specify the destination of the log messages. A simple configuration change will route the log messages to a flat file, a database, an e-mail address, the system event log, and so on. You can even filter messages based on their category or priority with a configuration change.
  • The sample app leverages the Enterprise Library's Exception Handling Application Block to manage exceptions. This application block lets you create named exception policies in a configuration file and use the policies in source code:


catch (Exception ex)
{
  if (ExceptionPolicy.HandleException(ex, "policy"))
  {
     throw;
  }
}

The effect of this code depends on the configuration file. The policy can specify that the exception is rethrown, replaced with a different exception, wrapped in another exception, or logged. If the exception should be thrown, HandleException will return True.

The S3 web service is a simple way to store and retrieve data over the Internet. But if you already have access to an FTP site and can live with the security drawbacks, you can use the .NET 2.0 FtpWebRequest class instead. In either case, the .NET 2.0 GZipStream class is a convenient way to compress data to conserve bandwidth and storage space. The Microsoft Enterprise Library contains some invaluable Application Blocks that simplify application development, and let many important modifications be accomplished with configuration updates rather than code changes. The cryptography, logging, and Exception Handling Application Blocks simplified the sample app's design and coding. I recommend you download the Enterprise Library and familiarize yourself with all of its capabilities. And if you ever troubleshoot Internet applications, you're "flying blind" without a network capture and analysis tool like Packetyzer.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.