Improve Manageability Improve Manageability Microsoft.NET

Download Improve Manageability Improve Manageability Microsoft.NET

Post on 26-Mar-2015




5 download

Embed Size (px)


<ul><li>Slide 1</li></ul><p>Improve Manageability Improve Manageability Microsoft.NET Slide 2 .NET 2004/03/24 2.NET Microsoft MCSD/MCSE/MCDBA Rational OOAD Lotus Notes principle CLP/CLI J2EE Slide 3 .NET 2004/03/24 3.NET.NET Slide 4 .NET 2004/03/24 4 Introduction Configuration Management Exception Management Enterprise Instrumentation Framework Logging Application block Slide 5 .NET 2004/03/24 5 Application Architecture UI Components UI Process Components Data Access Components Business Workflows Business Components Users Business Entities Service Agents Service Interfaces Data Sources Services Operational Management Security Communication Slide 6 .NET 2004/03/24 6 Benefits of Improving Manageability Development phase Pinpoint bugs, performance issues Deployment phase Configurable, Flexible Operation phase Know what is going on in applications and able to manage them. Provide sufficient information to track down what is going wrong. Slide 7 .NET 2004/03/24 7 Application Management Challenges For the Support Organization Application faults or issues are often detected first by users Support staff need structured application information Most tracing solutions are not intended for production deployment scenarios Distributed applications magnify these challenges Events fired from an application across multiple servers are hard to correlate Slide 8 .NET 2004/03/24 8 Application Management Challenges For the Development Organization No unified instrumentation API Developers are forced to use different eventing, tracing, or logging solutions Or, more often, avoid instrumentation entirely Firing an event must be as simple and low- profile as possible Developers are often forced to determine event routing within code Slide 9 Configuration Management Application Block - CMAB Slide 10 .NET 2004/03/24 10 App/Web.config Problems Simple data Configuration.AppSettings[ConnString] Not secure Read only File storage only, not Flexible... Slide 11 .NET 2004/03/24 11 Purpose of Configuration Management Application Block (CMAB) Purpose: Standardize interface to Read/Write application configuration data Supports complex data structures Pluggable Storage provider Pluggable Signing &amp; Encryption provider Cache configuration data Slide 12 .NET 2004/03/24 12 Conceptual Design Your Application Your Application Application XML Configuration - XML File - SQL Server - Registry - Custom Configuration Manager Configuration Storage Provider Data Protection Provider Data Protection Provider - DPAPI - BCL - Custom Caching Service Configuration Section Handlers Data Protection Provider Data Protection Provider Data Protection Provider Data Protection Provider Storage Service Provider Pluggable Handler, Storage/protection provider Slide 13 .NET 2004/03/24 13 CMAB Usage //Read Default section, i.e. first section, HashTable data format configData = ConfigurationManager.Read(sectionName); foreach( string key in configData.Keys ) list.Items.Add( new ListPair( key, configData[ key ] ) ); // Write ConfigurationManager.Write( sectionName, configData ); freaky chakra Slide 14 .NET 2004/03/24 14 Handler and Provider In the app.config / web.config Define Configuration Section Handler Define Configuration Storage Provider Define Data Protection Provider Define Cache parameters Slide 15 .NET 2004/03/24 15 CMAB configuration freaky chakra Slide 16 .NET 2004/03/24 16 Configuration Setting Schema Slide 17 .NET 2004/03/24 17.NET Support for Configuration Management Slide 18 .NET 2004/03/24 18 Implementation of ConfigurationManager //Configuration manager Read() &amp; Write() public static object Read( string sectionName ) { cacheSettings = CacheFactory.Create( sectionName ); configReader = StorageReaderFactory.Create( sectionName ); configSectionNode = configReader.Read(); sectionHandler = ConfigSectionHandlerFactory.Create( sectionName ); config = sectionHandler.Create( null, null, configSectionNode ); cacheSettings[ cacheSettings.SectionName ] = config; } public static void Write(string sectionName, object configValue) { storageReader = StorageReaderFactory.Create( sectionName ); storageWriter = (IConfigurationStorageWriter)storageReader; sectionHandler = ConfigSectionHandlerFactory.Create( sectionName ); sectionHandlerWriter = (IConfigurationSectionHandlerWriter)sectionHandler; xmlNode = sectionHandler.Serialize( configValue ); storageWriter.Write( xmlNode ); } Slide 19 .NET 2004/03/24 19 Implementation of ConfigurationSectionHandler public object Create(object parent,object configContext,XmlNode section) { foreach(XmlNode configChildNode in section.ChildNodes) { if (configChildNode.Name == "configSection" ) ProcessConfigSection( configChildNode, out sectionSettings, configSettings ); } void ProcessConfigSection( XmlNode configChildNode,...) { foreach(XmlNode sectionChildNode in configChildNode.ChildNodes) { switch ( sectionChildNode.Name ) { case "configCache" : ProcessConfigCacheSection( sectionChildNode, ); break; case "configProvider" : ProcessConfigProviderSection( sectionChildNode, ); break; case "protectionProvider" : ProcessProtectionProvider( sectionChildNode, ); break; } Slide 20 .NET 2004/03/24 20 Implementation of XmlHashtableSectionHandler object Create(object parent, object configContext, XmlNode section) { if( section.ChildNodes.Count == 0 ) return new Hashtable(); XmlSerializableHashtable xmlHt = (XmlSerializableHashtable)_xmlSerializer.Deserialize( new XmlNodeReader( section ) ); return xmlHt.InnerHashtable; } XmlNode Serialize(object value) { StringWriter sw = new StringWriter( ); _xmlSerializer.Serialize( sw, new XmlSerializableHashtable( (Hashtable)value ) ); XmlDocument doc = new XmlDocument(); doc.LoadXml( sw.ToString() ); return doc.DocumentElement; } Slide 21 .NET 2004/03/24 21 Implementation of XmlFileStorage public XmlNode Read() { XmlDocument xmlDoc = new XmlDocument(); LoadXmlFile(...); XmlNode sectionNode = xmlDoc.SelectSingleNode(@"/configuration/" + SectionName); if( _isSigned || _isEncrypted ) { XmlNode encryptedNode = sectionNode.SelectSingleNode( "encryptedData" ); XmlNode signatureNode = sectionNode.SelectSingleNode( "signature" ); byte[] hash =_dataProtection.ComputeHash( Encoding.UTF8.GetBytes( sectionData ) ); //Compare the hashes encryptedBytes = Convert.FromBase64String( sectionData ); decryptedBytes = _dataProtection.Decrypt( encryptedBytes ); sectionData = Encoding.UTF8.GetString( decryptedBytes );... } return section data; } Slide 22 .NET 2004/03/24 22 CMAB Summary Standard Read/Write interface to access application configuration data Supports complex data structures Pluggable Storage provider for flexibility Pluggable Signing &amp; Encryption provider for security Cache configuration data for efficiency Slide 23 Exception Management Application Block - EMAB Slide 24 .NET 2004/03/24 24 Exception Management Application Block (EMAB) Purpose: Make it simple and powerful to report that an exception has occurred (1 line of code) Make it extensible and flexible to log errors however you want Problems solved: Often exception code is unique and repeated in every application Changing how your exceptions are logged meant changing your code Difficult to pass context information up the stack Slide 25 .NET 2004/03/24 25 Solution Concept YourApplication Publisher (event log) Exception Manager Exception Publisher(database) Publisher (text file) Publisher(email) Slide 26 .NET 2004/03/24 26 EMAB Usage Publishing an exception: Pluggable Publishers of exception info Event Log as default. try {} catch (Exception ex) { ExceptionManager.Publish(ex); } Slide 27 * Means all exceptions, + means the exception and its descendants"&gt; .NET 2004/03/24 27 EMAB Configuration-1 * Means all exceptions, + means the exception and its descendants Slide 28 "&gt; .NET 2004/03/24 28 EMAB Configuration-2Slide 29 .NET 2004/03/24 29 Logical Design Slide 30 .NET 2004/03/24 30 BaseApplicationException The exception block provides a base application exception class from which all your exceptions inherit. Has contextual information like MachineName, Thread, Login, DateTime, AppDomain Has additional information collection for inserting custom pieces of data class HeartAttackException : BaseApplicationException {... } Slide 31 .NET 2004/03/24 31 Encounter An Exception Throw an customized exception Your custom exceptions now have additional information passed up the stack. publish your exception if you are at the top of the stack HeartAttackException ex; ex = new HeartAttackException(); ex.AdditionalInformation.Add("BloodPressure", "120/300"); ex.AdditionalInformation.Add("Pulse", 85); ex.AdditionalInformation.Add("WhiteCount", 1000); throw ex; Slide 32 .NET 2004/03/24 32 Publish an Exception private void btnLogon_Click() { try { if ( DoLogon(txtUserName.Text, txtPassword.Text) == true ) MessageBox.Show("Your Logon Was Successful"); else MessageBox.Show("Logon failed); } catch( LogonException lex){ // publish Exception using ExceptionManager ExceptionManager.Publish( lex ); MessageBox.Show(lex.Message); } private bool DoLogon(string userName, string password) { try{ using (FileStream fs = new FileStream( "Accounts.dat",)) { return CheckUserDetails(fs, userName, password); } catch (FileNotFoundException ex ) { throw new LogonException( "Internal Failure. User file not found", ex ); } Slide 33 .NET 2004/03/24 33 Tips for exception blocks Throw an customized exception Remember to publish your exception if you are at the top of the stack Do not re-throw it. If you choose to ignore the above tip, at least add exception handling into the Thread_UnhandledException event (winforms) Application_Error event (webforms) To display an exception to the user, you still need a MessageBox call. ExceptionManager.Publish just records it. Slide 34 .NET 2004/03/24 34 Implementation of BaseApplicationException [Serializable] public class BaseApplicationException : ApplicationException { public BaseApplicationException() : base() { InitializeEnvironmentInformation(); } public BaseApplicationException(string message) : base(message) { InitializeEnvironmentInformation(); } public BaseApplicationException(string message,Exception inner) : base(message, inner) { InitializeEnvironmentInformation(); } private void InitializeEnvironmentInformation() { machineName = Environment.MachineName; threadIdentity = Thread.CurrentPrincipal.Identity.Name; windowsIdentity = WindowsIdentity.GetCurrent().Name;... } Slide 35 .NET 2004/03/24 35 Implementation of ExceptionManager void Publish(Exception exception, NameValueCollection additionalInfo) { ConfigurationSettings.GetConfig(...); PublishToDefaultPublisher(exception, additionalInfo); // or foreach(PublisherSettings Publisher in config.Publishers) PublishToCustomPublisher(exception, additionalInfo, Publisher); } void PublishToCustomPublisher(...) { if (publisher.ExceptionFormat == PublisherFormat.Xml) { IExceptionXmlPublisher XMLPublisher = (IExceptionXmlPublisher)Activate(publisher.AssemblyName, publisher.TypeName); XMLPublisher.Publish(SerializeToXml(exception, additionalInfo),publisher.OtherAttributes); } else { IExceptionPublisher Publisher = (IExceptionPublisher)Activate(publisher.AssemblyName, publisher.TypeName); Publisher.Publish(exception, additionalInfo, publisher.OtherAttributes); } Slide 36 .NET 2004/03/24 36 Implementation of DefaultPublisher Get publisher s source Get custom exception info Get BaseApplicationException info Get stack trace Write to target source Slide 37 .NET 2004/03/24 37 Summary Register custom event sources by running installutil on your DLL Understand exception management best practices Build a dump to text file publisher for development Slide 38 Enterprise Instrumentation Framework EIF Slide 39 .NET 2004/03/24 39 EIF Goals Monitor and Troubleshoot applications built on the.NET Framework Integrate and unify disparate tracing, eventing, and logging technologies Support Distributed Environment Integrate with existing management tools and products Slide 40 .NET 2004/03/24 40 EIF Core Features Unified Instrumentation API Unified model for tracing, eventing, and logging Leverages WMI Infrastructure (System.Management.Instrumentation) Structured WMI Event Schema Manageability contract between developers, quality assurance, and support staff Configurable At-Source Event Filtering Fire via WMI, Event Log, Windows Event Trace Minimal cost for leaving instrumentation in production build High-speed Diagnostic Tracing Enables problem management for production applications Enables tracing user requests along application business processes or services Slide 41 .NET 2004/03/24 41.NET Application Instrumentation API Event Filtering Event Filtering WMI Event Trace Event Log Trace Log Event Log WMI Subscriber Configuration Event Schema Application Object Event Source Trace Reader Single Application Server Event Sink Slide 42 .NET 2004/03/24 42 Distributed Servers Distributed.NET Application Application Server Event Source Event Routing WMI Event Trace Event Log Trace Log Event Log WMI Subscriber Trace Reader Event Source Event Routing WMI Event Trace Event Log Trace Log Event Log WMI Subscribers Trace Reader Management Server Management Suite Mgmt Event Repository SQL Trace Event Repository Event Sink Slide 43 .NET 2004/03/24 43 EIF Usage Assemblies Microsoft.EnterpriseInstrumentation Microsoft.EnterpriseInstrumentation.Schema System.Configuration.Install Event sources Implicit event source (application) TraceMessageEvent.Raise(something happened"); Explicit event source Slide 44 .NET 2004/03/24 44 Raise Event by Implicit Event Source using Microsoft.EnterpriseInstrumentation; using Microsoft.EnterpriseInstrumentation.Schema; class MyComponent { // some method public void DoWork() { // Raise an event TraceMessageEvent.Raise(something happened"); } Slide 45 .NET 2004/03/24 45 EIF Explicit Event Source // using statements... class MyComponent { private static EventSource myEventSource = new EventSource(MyEventSource"); public void DoWork() { TraceMessageEvent.Raise(myEventSource, Important message-1"); // or TraceMessageEvent e = new TraceMessageEvent(); e.Message = Important message-2"; myEventSource.Raise(e); } Slide 46 .NET 2004/03/24 46 EIF Configuration Files Default name EnterpriseInstrumentation.config Default locations *.exein the same directory as executable Web applicationsroot of Web Name and location configurable Define in web.config or app.config Use appSettings section Slide 47 .NET 2004/03/24 47 EIF Configuration Definition and control of: Event source Event sink WMI, event log, and Windows event tracing Event categories, describing a set of related events Event filters, which map event sources to specific event categories and event sinks FilterBinding Event Category Event Sinks Event Filter Event Source Events Slide 48 .NET 2004/03/24 48 EIF Event Schemas Events in the standard event schema Administrative events Audit events Error events Trace events Custom event schemas Add ad...</p>