...

/

Logging During Development and Runtime

Logging During Development and Runtime

Learn about logging using .NET’s Debug and Trace classes, which allow developers to track application behavior and errors.

Once we believe all the bugs have been removed from our code, we will compile a release version and deploy the application so people can use it. But no code is ever bug-free; unexpected errors can occur during runtime.

End users are notoriously bad at remembering, admitting to, and accurately describing what they did when an error occurred. We should not rely on them accurately providing useful information to reproduce the problem so that we can understand what caused the problem and then fix it. Instead, we can instrument our code, logging events of interest.

Understanding logging options

.NET includes some built-in ways to instrument our code by adding logging capabilities. But logging is where third parties have created a rich ecosystem of powerful solutions extending what Microsoft provides. We cannot make specific recommendations because the best logging framework depends on our needs. Some common ones are in the following list:

  • Apache log4net

  • NLog

  • Serilog

Press + to interact

Instrumenting with Debug and Trace

Two types can be used to add simple logging to our code: Debug and Trace. Before we delve into them in more detail, let’s look at a quick overview of each one:

  • The Debug class adds logging that gets written only during development.

  • The ...