38
In this session, you will learn to: Configure an assembly by using the Configuration, Configuration Element, Configuration Section classes, and configuration base types available in the .NET Framework 2.0 Perform various installation tasks related to assembly installation Describe what event logs are and how to manage them Manage application processes Monitor and customize application performance Debug applications by using Visual Studio 2005 Trace applications by using .NET Framework 2.0 Objectives

Net framework session03

Embed Size (px)

Citation preview

Page 1: Net framework session03

In this session, you will learn to:

Configure an assembly by using the Configuration,

Configuration Element, Configuration Section classes, and

configuration base types available in the .NET Framework 2.0

Perform various installation tasks related to assembly

installation

Describe what event logs are and how to manage them

Manage application processes

Monitor and customize application performance

Debug applications by using Visual Studio 2005

Trace applications by using .NET Framework 2.0

Objectives

Page 2: Net framework session03

What are Configuration Files?

A configuration file is an XML file that contains specific

configuration settings for an application.

When you configure a configuration file for an application, it

eliminates the need to recompile the application every time a

setting changes.

Configuration files can be of the following types:

Web.config

App.config

Machine.config

Configuring an Assembly by Using Configuration Types

Page 3: Net framework session03

Access and Manage Configuration Settings

You can access and manage the configuration settings of a

computer, an application, or resources by using the

Configuration class.

You need to use the ConfigurationManager class to read

the data from a configuration file into an object of the Configuration class.

Configuring an Assembly by Using Configuration Types (Contd.)

Page 4: Net framework session03

Manage Configuration Elements

A configuration element is an XML tag in a configuration file

that contains a string value, attributes, or other child elements.

Configuration elements can be programmatically managed by

using the following classes:

ConfigurationElement

ConfigurationElementCollection

ConfigurationElementProperty

Configuring an Assembly by Using Configuration Types (Contd.)

Page 5: Net framework session03

Manage Sections in Configuration Files

The following configuration section classes can be used to

manage sections within the configuration files:

ConfigurationSection: Adds a custom section to the

configuration file.

ConfigurationSectionCollection: Reads all the

configuration sections in a configuration file.

ConfigurationSectionGroup: Represents a group of related

configuration sections in a configuration file.

ConfigurationSectionGroupCollection: Represents a

collection of ConfigurationSectionGroup objects.

Configuring an Assembly by Using Configuration Types (Contd.)

Page 6: Net framework session03

Create a Custom Type to Validate Configuration Values

It is important that the configuration values in the configuration

file are valid in type as well as in range.

You can implement the following interfaces to programmatically

access application settings, specific sections within a

configuration file:

IConfigurationSectionHandler

ISettingsProviderService

IApplicationSettingsProvider

Configuring an Assembly by Using Configuration Types (Contd.)

Page 7: Net framework session03

Configure the Run-time Version of .Net Framework

You need to configure the runtime version on the computer to

execute the applications created in the .NET Framework.

To define the runtime version, you can use the supportedRuntime and compatibilityversion

elements in the configuration file of the corresponding

application.

Bootstrapping is the process of installing and configuring the

CLR by using various deployment tools.

Performing Installation Tasks

Page 8: Net framework session03

Configure the Location, Version, and Search Path of an

Assembly

You should configure the location and version of all the

assemblies you reference in your application so that the CLR

is able to locate the assemblies easily.

You can either use mscorcfg.msc or codebase and probing

element to configure assemblies.

Performing Installation Tasks (Contd.)

Page 9: Net framework session03

Configuring an Application by Using Mscorcfg.msc

Mscocrfg.msc allows you to manage and configure assemblies

in the global assembly cache, adjust code access security

policy, and perform remoting services.

You can run this utility at the command prompt. You can also

access this utility from Start Menu, Control Panel,

Administrative Tools, Microsoft .NET Framework 2.0

Configuration.

You can configure applications using mscocrfg.msc under

administrator control.

Performing Installation Tasks (Contd.)

Page 10: Net framework session03

Configure Concurrent Garbage Collection

Garbage collection is a feature provided by the .NET

Framework to automatically manage the allocation and release

of memory for the managed objects in an application.

Garbage collector can be configured in the following modes:

Concurrent mode: In this mode, the garbage collector runs in a

process space that is different from the default domain of the

assembly. As a result, the execution of assembly does not have to

pause for garbage collection.

Non-Concurrent mode: In this mode, garbage collection happens

on the same thread as the thread running user code.

Performing Installation Tasks (Contd.)

Page 11: Net framework session03

Register Remote Objects by Using Configuration Files

You can use the configuration files for registering remote

assemblies. This eliminates the need for recompiling the code.

The remote assemblies communicate with each other through

channels.

Channels are objects that transport messages between

applications across remoting boundaries.

Performing Installation Tasks (Contd.)

Page 12: Net framework session03

What is bootstrapping?

Just a minute

Answer

The process of installing and configuring the CLR by using

deployment tools is called bootstrapping.

Page 13: Net framework session03

What are Event Logs?

Event logs are similar to a catalog where you can store entries

for particular activities and actions that occur within your

application.

Windows maintains the following types of event logs:

System Event Log: Logs information that takes place on system

level hardware.

Security Event Log: Logs security level events.

Application Log: Contains events logged by applications or

programs.

Event logs can be viewed programmatically by using the

EventLog class.

EventLogPermission class allows you to specify code access

security permissions for event logs.

Managing an Event Log

Page 14: Net framework session03

The following code example demonstrates the creation of a

custom event log and a custom event source:

class MyEventLog {

public static void Main() {

//Create the source, if it does not already

exist.

if (!EventLog.SourceExists("MySource")) {

EventLog.CreateEventSource("MySource","MyNewLog");

Console.WriteLine("CreatingEventSource"); }

//Create an EventLog instance and assign its

source.

EventLog myLog = new EventLog();

myLog.Source = "MySource";

myLog.WriteEntry("Writing to event log.");

} }

Managing an Event Log (Contd.)

Page 15: Net framework session03

How Entries Are Read and Written in an Event Log?

The EventLog class can be used to read and write entries in

existing logs.

The properties of the EventLog class are:

Log

MachineName

Entries

Source

WriteEvent

WriteEntry

Managing an Event Log (Contd.)

Page 16: Net framework session03

How do you write to an event log?

Just a minute

Answer

The EventLog class provides properties Source, WriteEvent,

and WriteEntry that are used to write entries to an existing event

log.

Page 17: Net framework session03

How a List of All Running Processes Is Retrieved?

The GetProcesses method of the Process class is used to

retrieve the list of all the processes running on the computer.

It returns an array that lists all the running processes.

How Information About the Current Process Is Retrieved?

The GetCurrentProcess method of the Process class is

used to retrieve information about the current process.

How a List of All Modules Used by a Process Is Retrieved?

The Process class provides the Modules method that can be

used to retrieve a list of all the modules used by a process.

Working with Application Processes

Page 18: Net framework session03

How an Application Process Is Started and Stopped?

The Start method is used to start a process.

The Kill method stops the specified process immediately. It

releases the resources allocated to the process.

Working with Application Processes (Contd.)

Page 19: Net framework session03

Monitoring Application Performance by Using the

Performance Monitor

In Windows 2000 and Windows XP, the Performance Monitor

is used to examine the performance of your applications.

The System Monitor in the Performance Monitor is used to

monitor the execution of processes.

Each monitored resource in the Performance Monitor is called

a performance object.

Each performance object has certain performance counters

associated with it.

Managing Application Performance

Page 20: Net framework session03

How Performance Information Is Customized by Using

Performance Counter Classes?

The System.Diagnostics namespace provides several

classes that can be used to create custom performance

counters.

The important classes are:

PerformanceCounter: Evaluates feedback from existing

counters.

PerformanceCounterCategory: Creates or removes custom

categories.

CounterCreationData: Creates multiple counters in a category

and specifies their types.

Managing Application Performance (Contd.)

Page 21: Net framework session03

What is Debugging?

The process of finding and correcting the defects in a block of

code is called debugging.

Debugging helps you resolve several kinds of errors. These

errors can be divided into the following types:

Syntax error

Run-time error

Logic error

The Microsoft CLR Debugger is run to debug an application.

The Microsoft CLR Debugger is installed when you install

Visual Studio 2005.

It is part of the Visual Studio 2005 Software Development Kit

(SDK).

Debugging Applications

Page 22: Net framework session03

How the Debugger Class Is Used to Debug

Programmatically?

The .NET Framework provides the Debugger class, which

helps to launch the Visual Studio 2005 debugger

programmatically.

The primary methods associated with the Debugger class:

Break: This method signals a breakpoint to an attached

debugger, with a user breakpoint event.

Launch: This method launches a debugger and attaches it to the

process.

Log: This method posts a message for the attached debugger.

Debugging Applications (Contd.)

Page 23: Net framework session03

The following code sample shows the implementation of the

Debugger class:

private void BreakInCode()

{

BooleanSwitch assertSwitch = new

BooleanSwitch("BreakOnAssert", "");

if (assertSwitch.Enabled)

{

if (Debugger.IsAttached)

Debugger.Break();

else

Debugger.Launch();

}

}

Debugging Applications (Contd.)

Page 24: Net framework session03

How the Debug Class Is Used to Debug Programmatically?

The Debug class provides a set of methods and properties that

help in debugging code.

Methods in the Debug class can be used to print debug

information.

A few primary methods associated with the Debug class:

Assert: This method checks a condition. If the condition is false,

the method displays a message.

WriteLine: This method writes information about the debug to

the trace listeners in the Listeners collection.

WriteLineIf: This method checks a condition, and if the

condition holds true, the method writes information about the debug to the trace listeners in the Listeners collection.

Debugging Applications (Contd.)

Page 25: Net framework session03

How User-Defined Types Are Configured by Using

Debugger Attributes?

The Debugger attributes are a set of classes that can be used

to configure user-defined types in a debugger.

How Call Stacks Are Managed by Using the StackFrame

and StackTrace Classes?

StackFrame Class: Provides information about a stack frame.

A stack frame is the physical representation of a function call

on the call stack for the current thread.

The members of the StackFrame class are:

GetFileName method: This method is used to retrieve the name

of the file that has provided the code in execution.

GetFileLineNumber method: This method is used to retrieve

the line number in the file that contains the code currently in

execution.

Debugging Applications (Contd.)

Page 26: Net framework session03

What are the different kinds of errors?

Just a minute

Answer

The different kinds of errors are syntax errors, runtime errors,

and logic errors.

Page 27: Net framework session03

What Is Application Tracing?

Application tracing refers to monitoring an application and

recording information about events as they occur in the

application.

Trace commands such as Trace.Write, Trace.TraceInformation,

Trace.TraceError, Trace.Indent, and Trace.Assert enable a

user to trace an application.

The .NET Framework provides the System.Web.TraceContext

class for a Web environment.

For a Windows environment, the .NET Framework provides

the System.Diagnostics.Trace and System.Diagnostics.Debug

classes.

Tracing Applications

Page 28: Net framework session03

How Applications Are Traced Programmatically by Using the Trace Class?

The Trace class provides a set of methods and properties that

help you to trace applications programmatically.

The members of the Trace class are:

Assert

WriteIf

Fail

Write

WriteLine

WriteLineIf

With the help of a configuration file, you can use the Trace

class programmatically in a console application to implement

tracing.

Tracing Applications (Contd.)

Page 29: Net framework session03

How the Source of Tracing Is Identified by Using the TraceSource Class?

The .NET Framework provides the TraceSource class that

helps to trace the execution of code and associate trace

messages with their source.

The members of the TraceSource class are:

TraceEvent

TraceData

TraceInformation

Name

Switch

Listeners

With the help of a configuration file, you can configure and implement the TraceSource class.

Tracing Applications (Contd.)

Page 30: Net framework session03

You can also implement the TraceSource class by using C#

to trace an application and associate trace messages with

their source.

The following code sample shows the implementation of the

TraceSource class:

using System;

using System.Diagnostics;

class Tracetest {

static TraceSource tS = new

TraceSource("TraceTest");

static void Main(string[] args) {

tS.Listeners["console"].TraceOutputOptions |=

TraceOptions.Callstack;

tS.TraceEvent(TraceEventType.Warning, 1);

} }

Tracing Applications (Contd.)

Page 31: Net framework session03

How the Tracing Output Is Configured by Using Trace

Switch Classes?

The trace switches are used to activate, deactivate, and filter

the tracing output.

The two types of trace switch classes are:

TraceSwitch

BooleanSwitch

The configuration file is used to add a switch and define its

value, remove a switch, or get rid of all switches set up initially

by the application.

Tracing Applications (Contd.)

Page 32: Net framework session03

The following code sample shows the implementation of the

TraceSwitch class.

using System;

using System.Diagnostics;

class Tracetest

{

// Create a TraceSwitch to use in the entire

application class level declaration

private static TraceSwitch appSwitch = new

TraceSwitch("mySwitch", "Switch in config

file");

Tracing Applications (Contd.)

Page 33: Net framework session03

public static void Main(string[] args)

{

Console.WriteLine("Trace switch {0} configured

as {1}",

appSwitch.DisplayName,appSwitch.Level.ToString()

);

if (appSwitch.TraceError)

{

//...

}

}

}

Tracing Applications (Contd.)

Page 34: Net framework session03

How the Tracing Output Is Directed by Using Trace Listener

Classes?

Trace listeners are objects used to send the trace information

to an output location, such as a file, a printer, or the Visual

Studio 2005 output window.

The various trace listener classes are:

TraceListener

XmlWriterTraceListener

DelimitedListTraceListener

EventLogTraceListener

The TraceFilter class allows you to filter trace information

before sending it to an output location.

With the help of a configuration file, you can configure and implement the TraceListener class.

Tracing Applications (Contd.)

Page 35: Net framework session03

How Trace Information Is Categorized by Using the CorrelationManager Class?

The CorrelationManager class is used to group and

classify trace information for later analysis.

It is connected with the Thread class by the

CorrelationManager property.

The namespace for the CorrelationManager class is

System.Diagnostics, and the assembly is System (in

System.dll).

Tracing Applications (Contd.)

Page 36: Net framework session03

What is the TraceSource class?

Just a minute

Answer

The TraceSource class belongs to the System.Diagnostics

namespace and is used to trace the execution of code and

associate trace messages with their source.

Page 37: Net framework session03

In this session, you learned that:

The application configuration file helps specify configuration

settings for applications other than Web applications.

mscorcfg.msc is a .NET Framework utility that helps configure

an application so that the application can use the externally

referenced assemblies.

Garbage collection is a feature provided by the .NET

Framework to automatically manage the allocation and release

of memory for the managed objects in an application.

Event logs contain entries to record important software and hardware events.

An instance of an application under execution is called a

process.

You need to manage the performance of your application to

identify operational issues and optimize performance.

Summary

Page 38: Net framework session03

Debugging helps you ensure that the code provides expected

results.

You can use tracing to monitor the code and the target points

where an application deviates from the normal functioning.

Summary (Contd.)