Project Description

This library allows development teams using Enterprise Library to deliver consistent log messages by using greatly simplified methods for writing messages to Enterprise Library Logging Application Block.

To prove the mantra "Flexibility Breeds Complexity" you need to look no further than logging frameworks, and specifically EntLib Logging. While there are many places to improve EntLib logging, this class library focuses on making writing common messages easier.

Some Features:

  • 100% Compatible with Enterprise logging and can be used interchangeably within the same application
  • No need to explicitly set Logger's internal methods before use (EntLib6)
  • Natural syntax with reasonable, but overridable, defaults
  • Timestamps reset from UTC to local time by default
  • Automatically capture a robust set of run-time context for severe messages
  • Exception details expose properties not normally exposed by ToString() and recursively include all inner exceptions
  • Allows parameterized messages without the clutter of String.format()
  • Encourages consistent generation of messages
  • Embedded error expansion

NOTE: This extension is a robust façade over the standard EntLib Logging::Logger class. It does not replace Enterprise Library. Your application still needs to be configured to use Enterprise Library.

Getting Started:

  1. Add a reference to HashTag.EntLib or download it from NuGet.org
  2. Add ‘using Microsoft.Practices.EnterpriseLibrary.Logging;’ (C#)
  3. Start Logging! (e.g. LogX.Info.Write(“Hello”);

Examples

Simple Message

LogX.Info.Write("Some really nice message");
LogX.Verbose.Write(“We got here with {0} pounds of gold”,lbCount);

Error Message

LogX.Error.Write("Help!");
LogX.Error.Catch(myException).Write(“Something really bad went down”);
LogX.Info.Catch(myHandledException).Write();
LogX.Warning.Category(“Payables”).Prop(“Account”,accountId).Write(“Authorization will expire in {0} days”, expirationDays);

Understanding the Syntax Pattern

This interface uses a pattern similar to .Net’s StringBuilder class. With StringBuilder you instantiate the builder, add a series of string fragments, then convert the builder into a string. With this interface you access the builder, (optionally) collect data you want to persist to log, and then write the message to the log.

The general pattern is: LogX.<level>.[<collector>[,<collector>…].<action>

LogX.Info.Write("First Message on {0}",DateTime.Now);

  1. Start with LogX (LogX replaces EntLib Logger class)
  2. Info – Log with TraceEventType.Information level
  3. Write – persist log entry with specific text. IMPORTANT: you must terminate each LogX entry with a call to one of the Write (or aliased ‘W’) methods to trigger writing the message to the log sink.

Notice the simplicity of the fluent syntax as compared to the traditional EntLib syntax:

Logger.Write(string.Format("First Message On {0}", DateTime.Now), "General", 8, 8, TraceEventType.Information);

Supported Levels

Hashtag.EntLib supports Enterprise Library log levels of Critical, Error, Warning, Information, and Verbose. It does not support the tracing levels of Start, Stop, Suspend, Resume, and Transfer. To use the non-supported levels, you can use the native Enterprise Library Logger class or a hybrid of EntLib and HashTag.

Defaults

HashTag.EntLib uses common sense defaults to prevent message creep when the code base is maintained over longer periods of time. It also helps provide consistency as developers roll across the project.

Category: “General” unless otherwise specified in the LogRequest

Timestamp: Local time

ActivityId: current Trace.CorrelationManager.ActivityId

Level

Default Priority

Default Event Id

.Critical (alias Cr)

1

1

.Error (alias .Er)

2

2

Warning (alias .Wa)

4

4

Information (alias .In)

8

8

Verbose (alias .Ve, .Dbg)

16

16

Of course, each of these defaults are easily overridden within fluent syntax.

Collectors

Collectors gather and assign data that will be persisted to the Enterprise Library configured log sink. These often simply set values in the underlying LogEntry object but some provide some more robust contextual information.

Collector

LogEntry Target

 

.Catch (alias Ex)

le.ErrorMessages

Also provides detailed exception information le.ExtendedProperties including all inner exceptions

.Category (alias .Ca)

le.Categories

Adds a category to entry’s categories collection. By default all messages have “General” as their category

.Collect (alias .Prop)

le.ExtendedProperties

Add an key/value pair to log entry’s dictionary

.EventId (alias .Id)

le.EventId

Set by default based onEventType.

.EventType (internal use)

le.Severity

Set during LogX.??? call

.HttpContext (alias .Http)

le.ExtendedProperties

Collects cookies, query string, server variables, and session variables. Collected by default on all Critical, Error, and Warning messages

.MachineContext(alias .Os)

le.ExtendedProperties

Collects OS, Host, memory, and related information. Collected by default on all Critical, Error, and Warning messages

.Priority (alias .Pr)

le.Priority

Set by default based on EventType. Remember EnterpriseLibrary.Logging uses lower priority numbers to mean higher more important.

.Stack (alias .St)

le.ExtendedProperties

Gets the current stack trace at the moment the message is created

.UserContext (alias .User)

le.ExtendedProperties

Collects both Thread and Http user principal. Collected by default on all Critical, Error, and Warning messages

*Severe (Critical, Error, Warning) messages automatically call HttpContext, MachineContext, and UserContext so the developer doesn’t have to remember to explicitly request those.

Actions

Actions terminate the builder and convert the built up values in the builder into something the caller can use. This is analogous to StringBuilder.ToString()

Action

 

Write (alias W)

Persist the message to Enterprise Library. The most common action.

ApplySettings

Take all builder values and assigned them to a LogEntry. Used mostly for internal purposes

Entry

Get a log entry with all the settings applied. Returned value can be used in EnterpriseLibrary logging calls (e.g. Logger.Write(myEntry))

Clearly Cryptic

As developers we are often looking for shortcuts. All Levels (e.g Critical, Error), Collectors (.Stack, .HttpContet), and Actions (Write) have one or two letter aliases. By example:

LogX.Error.Catch(myException).Collect(“environment”,”demo”).Write(“Record {0} blew up”,index);

becomes:

LogX.Er.Ex(myException).Prop(“environment”,”demo”).W(“Record {0} blew up”,index);

 

Last edited Oct 28, 2013 at 2:03 PM by HashtagDotNet, version 9