13. Diagnostics || C# 10 Flashcards

1
Q

What are the ‘preprocessor directives’?

A

Preprocessor directives are special instructions to the compiler that begin with the # symbol and must on entirely separate line. They execute before the main compilation takes place
~~~
#if TESTMODE
Console.WriteLine();
#endif
~~~
By using conditional compilation we can perform major refactoring under a conditional compilation directive, so we can switch between old and new versions and write libraries that can compile against multiple runtime versions, leveraging the latest features where available.
Another advantage - debugging code can refer to types in assemblies that are not included in deployment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Suppose I want to write a method that would be called only if we are in TESTMODE environment. How can I do that?

A

By adding ‘Conditional’ attribute above the method:
~~~
[Conditional(“TESTMODE”)]
static void LogStatus (string msg)
{

}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do you know about Debug and Trace classes?

A

Debug and Trace classes are static classes that provide basic logging and assertion capabilities. The two classes are very similar; the main difference is their intended use. The Debug class is intended for debug builds; the Trace class is intended for both debug and release builds.
You define the methods with either:
[Conditional(“DEBUG”)]
or
[Conditional(“TRACE”)]
Both Debug and Trace classes provide Write, WriteLine and WriteIf methods. The output is sent to the debugger’s output wondow : Debug.WriteIf(x>y, "x is greater that y");
The Trace class also provides the methods TraceInformation, TraceWarning and TraceError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a TraceListener?

A

The Trace class has a static Listeners property that returns a collection of TraceListeners instances. These are responsible for processing the content emitted by the Write, Fail and Trace methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

By default Trace.Listeners collection each item contains Default TraceListener. What are the main features of it?

A
  1. When connected to a debugger, such as Visual Studio, messages are written to the debug output window; otherwise, messages content is ignored;
  2. When the Fail method is called (or an assertion fails), the application is terminated;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lets assume I want to add logging to my program in 3 ways:
1. Log to a file;
2. Log to a console;
3. Log to Windows event log;

How can I achieve that with Trace class?

A

We should add 3 listeners:
~~~
// Clear the default listener:
Trace.Listeners.Clear();

// Add a writer that appends to the trace.txt file:
Trace.Listeners.Add(new TextWriterTraceListener(“trace.txt”));

// Obtain the console’s output stream, then add that as listener:
System.IO.TextWriter tw = Console.Out;
Trace.Listener.Add(new TextWriterTraceListener(tw));

// Set up a Window Event log source and then create/add listener.
//CreateEventSource requires administrative elevation, so this would typically be done in application setup:
if(!EventLog.SourceExists(“DemoApp”))
EventLog.CreateEventSource(“DemoApp”, “Application”);

Trace.Listeners.Add(new EventLogTraceListener (“DemoApp”))
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When logging we usually have 3 kinds of messages: Information, Errors and Warning. How can I get them written in Trace class?

A

First we need to create a TraceListener:
var tl = new TextWriterTraceListener(Console.Out);
Then we call 3 different methods for each type:
Trace.Write() or Trace.Fail() or Trace.Assert() - Information
Trace.TraceError - Error
Trace.TraceWarning - Warning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In some cases when using TraceListeners, the messages dont get logged to the file, why? What can we do to prevent that?

A

Some listeners, such as TextWriterTraceListener, ultimatelly writes to a stream which is a subject to caching, so therefore:
* The message might not appear in the output stream immediately;
* If not flushed before closing the app, we can loose what’s been cached (and not logged yet);

As general rule, call Close (it calls Flush, close file handlers and so on) before an application ends, and call Flush anytime you want to ensure that current message data is written.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to get all running processes, how to select one and then - stop it?

A

To get all running processes:
var processes = Process.GetProcesses()
Select one:
var selectedP = processes.WorkingSet64
Stop selected one:
selectedP.Kill()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the StrackTrace and StackFrame classes?

A

The StackTrace and StackFrame classes provide a read-only view of an execution call stack. You can obtain stack traces for the current thread (that is ProcessTread and not System.Threading.Tread objects) or an Exception object. Such information is useful mostly for diagnostic purposes.
StackTrace represents a complete call stack;
StackFrame represents a single method call within that stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What if for diagnostic purposes I wanted to get more info than Exception’s StackTrace property can suggest, what is my other option?

A

To create a StackTrace object an pass the exeption to the constructor. In that way we will get detailed view of where exactly the exception was thrown when the app is deployed and no debugging .pdb files are available for such info.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Windows Event Logs?

A

A centralized logging mechanism provided by the Win32 platform.
We can use the same Debug and Trace classes to write to a Window event log if we first register an EventLogTraceListener.
But with the EventLog class we can write directly to a Windows event log without using Trace or Debug.
There are 3 standard Windows event logs:
1. Application (most applications normally writes to)
2. System
3. Security

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can I write to and read from the Event Log?

A

To write to event log we need:
1. To choose to which of 3 event logs we want to write to;
2. If not created yet, create a source
3. Call EventLog.WriteEntry, passing a) log name, b) source name and c) message data

EventLog.CreateEventSource(“SourceName”, “Application”);
EventLog.WriteEntry(“SourceName”, “Services started”, EventLogEntryType.Information);

READING:
EventLog log = new EventLog(“Application”);
Console.WriteLine(log.Entries[0].Message);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are the Performance Counters?

A

Its a performance-monitoring infrastructure, which consists of a set of performace counters that the system and application expose.
They are grouped into the categories like “System”, “Processor” and so on. One group of performance counters monitor one aspect of the system or application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the Stopwatch class do?

A

The Stopwatch class provides a convenient mechanism for measuring execution times. Stopwatch, compared to DateTime.Now and Environment, uses the highest-resolution mechanism that the OS and hardware provide, which is typically less than a microsecond (compared to 15ms)
Stopwatch.StartNew();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is ‘dotnet-counters’ ?

A

The dotnet-counters tool monitors the memory and CPU usage of a .NET process and writes the data to the console (or a file)
To install:
dotnet tool install - - global dotnet-counters
To use:
dotnet-counters monitor System.Runtime - - process-id «ProcessId»
System.Runtime means that we want to monitor all counters under the System.Runtime category

17
Q

What are the traces in the ‘dotnet-trace’ command?

A

Traces- timestamped records of events in your program, such as a method being called or a database being queried. Traces can also include performance metrics and custom events and can contain local context such as the value of local variables
To install:
dotnet tool install - -global dotnet-trace
To start recording program’s event:
dotnet-trace collect - -process-id «ProcessId»
This collects CPU and .NET runtime events and writes to a file called trace.nettrace

18
Q

What does a dotnet-dump do?

A

A dump, sometimes called acore dump, is a snapshot of the state of a process’s virtual memory. You can dump a running process on demand, or configure the OS to generate a dump when an application crashes.
To install:
dotnet tool install - -global dotnet-dump
After install, you can initiate a dump on demand (without ending the process):
dotnet-dump collect - -process-id «ProcessId»
To analyze dump file:
dotnet-dump analyze «dumpfile»