370 likes | 384 Views
Explore key logging concepts and capabilities, implement scenarios using the Logging and Instrumentation Application Block, manage logs efficiently, and customize logging configurations.
E N D
Enterprise LibraryCaching Application Block Scott Densmore Software Design Engineer Peter Provost Software Design Engineer Ron JacobsProduct Manager
Describe the key logging concepts and capabilities Describe implementation of logging scenarios using the Logging and Instrumentation Application Block Demonstration Questions Agenda
Sound familiar? • Writing a component to make it simpler to log and trace application information • Different groups in your enterprise writing “new and improved” logging systems • Having multiple logging systems in different layers of the application • A component that logs locally, but doesn’t extend to a distributed logging system • Inflexible configuration options for logging • Recompiling code to change logging behavior
Logging Needs • You need to log business and operations data to various destinations, which are externally configurable • You need to provide tracing to support production debugging • You need to provide auditing for increased security • You need to be able to specify which messages go where, and how they are formatted • You need to be able to log messages to a wide variety of destinations
Logging & Instrumentation Application Block • Provides a simple model for logging events • Replaces the Enterprise Instrumentation Framework and the existing Logging Application Block • Configuration driven – you decide what messages are logged where at runtime. • Sinks provided include • Event Log • Database • Text File • MSMQ • Email • WMI • Create your own…
Scenarios • Populating and raising events from code • Populating a log message with additional context information • Tracing activities and propagating context information • Direct different event types to different sinks • Filter events based on category or priority • Configure logging to be done synchronously or asynchronously • Customize how logging messages are formatted
Populating and raising events Log a message Logger.Write(“Something of note”); How and where logging is done is determined by configuration Convenient overloads allow you to set additional properties Logger.Write(“Something of note”, “DataAccessCategory”); Logger.Write("Something of note", "General", 1, 200, Severity.Information, "Sample Entry");
Log Entries • The application block constructs a log entry to hold the information • Every log entry has the following properties • Message (required) • Category (default determined by configuration) • Priority (default -1) • Event ID (default 1) • Severity (default Severity.Unspecified) • Title (default “”) • You can create (and reuse) a log entry object, and pass it to the Logger.Write method
Creating Log Entries The application block creates the log entry Logger.Write(“Something of note”); The client code creates the log entry LogEntry log = new LogEntry(); log.Message = "My message"; log.EventId = 1; log.Category = "UI Events"; log.Priority = 2; log.Severity = Severity.Information; log.Title = "My title"; Logger.Write(log);
Populating a Log Message with Additional Context Information You can pass a dictionary of name-value pairs to the application block Hashtable customProperties = new Hashtable(); customProperties["key1"] = "value1"; customProperties["key2"] = "value2"; customProperties["key3"] = "value3"; Logger.Write("Something of note", "General", 1, 200, Severity.Information, "Sample Entry", customProperties); Output (Text Formatter)
Extra Information Providers DebugInformationProvider example Hashtable dictionary = new Hashtable(); DebugInformationProvider informationHelper; informationHelper = new DebugInformationProvider(); informationHelper.PopulateDictionary(dictionary); Logger.Write("Log entry with extra information", dictionary); Output (includes stack trace)
Tracing Activities and Propagating Context Tracing an application activity is a common task The application block provides support to track an activity by an activity identifier The activity identifier can be specified in the code, or generated by the application block Components inherit the caller’s activity id and log category A component that is called as part of an activity can temporarily override the activity identifier (nested activities) or category The application block automatically logs the start and end time of the activity
Tracing Example Activity tracing is provided by the Tracer class // Begin tracing activity using (new Tracer("UI Events")) { DoDataAccess(); } private void DoDataAccess() { // Trace nested activity using (new Tracer("Data Access Events“, “Data Access”)) { Logger.Write(“Troubleshooting message"); } }
Tracing Example Output Activity d9adc4bd-8883-4014-9696-e43d23f95201 Activity Data Access
Directing Different Event Types to Different Sinks Every log entry specifies a category (either explicitly in the code, or by inheriting the configured default) The application block examines the category to determine where to send the log entry (the destination) A destination specifies a formatter and a sink Categories and sinks are controlled through configuration Sinks shipped with application block: email, event log, flat file, MSMQ, WMI (ETW coming soon!)
Directing Different Event Types to Different Sinks The Configuration Console is used to create categories and destinations
Directing Different Event Types to Different Sinks The default category is determined by a property of the Distributor Settings Messages that do not specify a category, or specify an unrecognized category, will assigned the default
Filter Events Based on Category or Priority Filtering occurs before a log entry is delivered to the distribution strategy Each message has a priority (either set explicitly through code or the inherited default, -1) You can configure the Client Settings to cause messages below a specified priority to be filtered out You can configure the Client Settings to only log messages of a specified category You can configure the Client Settings to filter out log messages of a specified category
Filter Events Based on Category or Priority In the following example, messages of priority 0 or 1 will not be logged
Filter Events Based on Category or Priority The CategoryFilterSettings property of the Client Settings determines if categories are logged or filtered out In the following example, messages for category UI Events are not logged
Configure Logging to be Done Synchronously or Asynchronously The client settings determine the distribution strategy Synchronous logging is performed in the process of the client Asynchronous logging is implemented using Microsoft Message Queuing (MSMQ) For asynchronous logging, an application block Windows Service pulls log entries off of the queue and sends them to the application block You can extend the application block and provide a custom distribution strategy
Configure Logging to be Done Synchronously or Asynchronously
Configure Logging to be Done Synchronously or Asynchronously The client settings determine the distribution strategy All messages for a client use the same strategy
Configure Logging to be Done Synchronously or Asynchronously For asynchronous logging, the MSMQ distribution strategy includes a property for the queue name
Configure Logging to be Done Synchronously or Asynchronously The MSMQ Distributor Service must be configured with the appropriate queue name (must match the client) Using the Configuration Console, open the file MsmqDistributor.exe.config Add the MSMQ Distributor Service to the Distributor Settings Set the MsmqPath property to match the client’s queue name
Customize How Logging Messages are Formatted Formatters accept a log entry and return a formatted string The application block sends the formatted string to the sink The Text Formatter uses a template to produce the formatted string You can have multiple Text Formatters in your application configuration, each with a custom template You can write a custom formatter
Customize How Logging Messages are Formatted Log entries are routed to a destination based upon their category The destination specifies the formatter to use and the sink which will receive the formatted log entry
Customize How Logging Messages are Formatted The Text Formatter exposes the template through a configuration property
Customize How Logging Messages are Formatted The Template Editor allows you to easily change how the log entry is formatted Tokens are substituted with log entry values
View/Application Share: Demonstration • [Live Meeting View/Application Share. Use Live Meeting > Edit Slide Properties... to edit.]
Enterprise Library v1 Caching Exceptions Legend Security Data Access Logging Dependency Plug-in Crypto Configuration Config Tool
Announcing: Enterprise Library 1.0 Download it Today! http://www.microsoft.com/practices
patterns & practices Live! • 3/17 Enterprise Library Exception Handling Application Block • 3/22 Enterprise Library Cryptography Application Block • 3/24 Enterprise Library Security Application Block • 3/28 Building your own block • 3/31 Enterprise Library Applied http://www.pnplive.com
http://www.microsoft.com/practicesEnterprise Library Communityhttp://go.microsoft.com/fwlink/?linkid=39209&clcid=0x09