Enterprise Library—Lab Exercises



Create and use a custom log sink

In this exercise you will add a custom sink to the logging configuration which will send the log entries to the Console standard output.  You will then reconfigure the application to monitor the log events in real-time.

First step

  • Open your solution file, and build the solution.
  • Create a custom log sink

    1. In Visual Studio add the following references;
       
      1. Microsoft.Practices.EnterpriseLibrary.Configuration.dll
        (All Blocks depend on the Configuration Application Block for handling configuration files etc.  You must include this assembly for the configuration of our custom sink).
      2. Microsoft.Practices.EnterpriseLibrary.Logging.dll
        (Contains the logging application block base classes).
      3. Add the following namespaces.
        using Microsoft.Practices.EnterpriseLibrary.Configuration;
        using Microsoft.Practices.EnterpriseLibrary.Logging;
        using Microsoft.Practices.EnterpriseLibrary.Logging.Sinks;
        using Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration;

         

    2. Add the following code to the ConsoleSink class.
    3. public class ConsoleSink:  LogSink
      {
          private LoggingConfigurationView configView;
       
          public override void Initialize(ConfigurationView configurationView)
          {
              this.configView = configurationView as LoggingConfigurationView;
          }
       
          protected override void SendMessageCore(LogEntry logEntry)
          {
              CustomSinkData sinkData;
              sinkData = (CustomSinkData) this.configView.GetSinkData(this.ConfigurationName);
       
              // Delimit each log entry
              Console.WriteLine((string) sinkData.Attributes["delimiter"]);
       
              // Write the formatted log entry to the Console
              Console.WriteLine(FormatEntry(logEntry));
          }

      }
    Note: The base class is LogSink, which mandates that you override two abstract methods (Initialize and SendMessageCore).  The Initialize method allows us to get access to the ConfigurationView (ie. a view of the configuration parameters as specified in the configuration files).  The SendMessageCore method is invoked by the logging infrastructure to send the message to the log sink.
     
    The ConsoleSink Classis expecting a parameter, named delimiter, be defined in the configuration of this custom log sink.
  • Select Build | Build Solution to compile the complete solution.
  • Using a custom sink

    1. Run the Enterprise Library Configuration tool.  From the Windows Start menu select All Programs | Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration.
    2. Select File | Open Application and browse to the App.config file.
    3. Right click on the Sinks node of the Logging and Instrumentation Application Block Distributor Settings.  Select the New | Custom Sink menu command.

    4. Select the Name field and rename the Custom Sink as Console Sink.
    5. Select the TypeName field and click the ellipses to display the Type Selector dialog.

    6. Click the Load an Assembly … button and browse to the EnoughPI.Logging.dll assembly here and click the Open button.
    7. Select the ConsoleSink class from the EnoughPI.Logging assembly and click the Ok button.

    8. Select the Attributes field and click the ellipses to display the NameValueItem Collection Editor.

    9. Click the Add button to add a new NameValueItem.  Set the Name field to delimiter, and the Value to a line of dashes (eg. ------------------).  Click the Ok button.

      Note: You will remember our ConsoleSink is expecting a parameter named delimiter, which is printed before each log entry is written to the console.
    10. Right click the General category of the Logging and Instrumentation Application Block Distributor Settings.  Select the New | Destination menu command.

       

    11. Set the following properties for the destination.
      • Formatter = Text Formatter,
      • Name = Console Destination, and
      • Sink = Console Sink.

       

    12. Add a similar destination to the Trace category.

      Select the File | Save All menu command to save the configuration. Close the Configuration Console.

    View the sink output

    In Visual Studio, select the project.  Select the Project | Properties … menu command to view the project Property Pages.  Select Common Properties | General and add change the Application Output Type to Console Application

    In Visual Studio, select the Debug | Start menu command to run the application.  Click the Calculate button.  The log entries will be displayed in the applications Console Window.