Enterprise Library 2.0 hands on Lab translation (4): Log Application Block (i)

Source: Internet
Author: User

Exercise 1: Add log records to your application

This exercise will demonstrate how to add logs and monitoring to an existing application (trace is a translation, not quite accurate) and configure TraceListeners with the Enterprise Library Configuration tool.

First step

Open the EnoughPI.sln project, the default installation path should be C:\Program Files\Microsoft Enterprise Library January Ex01\begin, and compiled.

Second Step about practice application

Select Debug | The Start without Debugging menu command and runs the application, which the ENOUGHPI program uses to calculate ∏ precision. Enter the precision you want in the NumericUpDown control and click the Calculate button.

Step three Add Log

1. Select Enoughpi Project, choose Project | Add Reference ... menu command, select Browse in the Open dialog box, and add the following assembly.

Microsoft.Practices.EnterpriseLibrary.Logging.dll;

The default location should be the C:\Program Files\Microsoft Enterprise Library January 2006\bin.

2. Select the Calc\calculator.cs file in Solution Manager, select View | Code menu command, and add the following namespaces.

Using Microsoft.Practices.EnterpriseLibrary.Logging;

3. Record the information when the calculation is complete add the following code to the Oncalculated method of the Calculator.cs file.

protected void OnCalculated(CalculatedEventArgs args)
{
  // TODO: Log final result
  LogEntry log = new LogEntry();
  log.Message = string.Format("Calculated PI to {0} digits", args.Digits);
  log.Categories.Add(Category.General);
  log.Priority = Priority.Normal;

  Logger.Write(log);

  if (Calculated != null)
    Calculated(this, args);
}

Creates a new log entry logentry and sets the parameters, using the static method of the Logger class write () to log to one or more tracelistener. Note that there is no category and priority using the hard coding to use constants, which are defined in the Constants.cs of enoughpi.logging:

public struct Priority
{
  public const int Lowest = 0;
  public const int Low   = 1;
  public const int Normal = 2;
  public const int High  = 3;
  public const int Highest = 4;
}
public struct Category
{
  public const string General = "General";
  public const string Trace  = "Trace";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.