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";
}