Partager via


Adding Application Code

The Logging Application Block is designed to support the most common scenarios for logging information. When adding your application code, refer to the scenarios in the Key Scenarios sections and select the ones that best suit your situation. Use the code that accompanies the scenario either as it is or adapt it as necessary.

First, prepare your application to use the Logging Application Block. The following procedure describes how to include the necessary Enterprise Library assemblies and elements in your code.

To prepare your application

  1. Set a reference to the Logging Application Block assembly:

    • If you are using C#, in Visual Studio, right-click References in Solution Explorer, and then click Add References to add references to the following assemblies:
      • Microsoft.Practices.Unity.dll
      • Microsoft.Practices.Unity.Interception.dll
      • Microsoft.Practices.ServiceLocation.dll
      • Microsoft.Practices.EnterpriseLibrary.Common.dll
      • Microsoft.Practices.EnterpriseLibrary.Logging.dll
    • If you are using Visual Basic®, double-click My Project in Visual Studio, click the References tab, and then click Add Reference to select the assembly. In the list of imported namespaces at the bottom of the tab, select the following check boxes:
      • Microsoft.Practices.Unity.dll
      • Microsoft.Practices.Unity.Interception.dll
      • Microsoft.Practices.ServiceLocation.dll
      • Microsoft.Practices.EnterpriseLibrary.Common.dll
      • Microsoft.Practices.EnterpriseLibrary.Logging.dll
  2. (Optional) To use elements from the Logging Application Block without fully qualifying the element reference, you can add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file.

    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
    
    'Usage
    Imports Microsoft.Practices.EnterpriseLibrary.Logging
    Imports Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation
    Imports Microsoft.Practices.EnterpriseLibrary.Logging.Filters
    

    Note

    For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.

    The ExtraInformation providers gather context information that is useful but not always necessary because it is expensive to collect. Examples are stack trace information and COM+ information. The ExtraInformation providers add the information to a dictionary. You can choose which providers to use (if any) and add the resulting dictionary to the LogEntry.ExtendedProperties property.

    Filters are optional. You only need to import the Microsoft.Practices.EnterpriseLibrary.Logging.Filters namespace if you are going to refer to specific filters in your application code.

  3. If you are using the DatabaseTraceListener class, you must also do the following:

    • Configure the application to use the Data Access Application Block. For more information, see The Data Access Application Block.

    • Execute the script named CreateLoggingDb.cmd (located in the Source\Blocks\Logging\Src\DatabaseTraceListener\Scripts folder) to create the Logging database.

      Note

      It is also a good idea to add a reference to the Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll to your project so that the assembly required at run time is copied to the output folder.

  4. Add the application code.