.NET Logging Best Practices and Tools

By Staff Contributor on October 23, 2020

Is your organization looking for a better way to log? Unfortunately, logging comes with several challenges. Especially when logs pile up with an immense amount of data. This makes extracting key metrics difficult without the right tools. Therefore, it’s vital one has an organized logging practice.

Today we’re taking a look at .NET logging. Firstly, I’ll introduce logging in the .NET framework, including libraries and the logging API. Thereafter, I’ll describe some of the best practices. Finally, I’ll introduce some software used to make .NET logging easier.

Logging in .NET’s Framework

Before we go further, it’s important you know about logging levels, libraries, and the API in .NET.

.NET has three main logging levels:

  • Error: This is a level under reservation for critical events. These often cause the system to crash.
  • Warning: This level warns the administrator of a possible problem. A warning might not make the application crash but could lead to critical errors.
  • Information: This level isn’t considered critical as it only gives information on what’s happening as the application runs. It provides extra information in case the logs are reviewed. .NET logging automates monitoring of errors and performance and allows for errors to be investigated after they’ve happened. It can also gather user statistics to discover research usage patterns, behavior, and demographics.

.NET Logging Libraries

Next, let’s look at the many logging libraries available supporting the .NET framework. For example, NLog, Serilog, NSpring, and log4net. These are the libraries currently dominating .NET logging.

NLog Logging Library

Firstly, NLog is an open-source logging library for .NET. It simplifies writing to various destinations, including file, console, email, and database. Advantageously, NLog is easy to configure both through the configuration file and programmatic configuration.

Serilog

Similarly, we have Serilog. It’s a structured logging library for Microsoft .NET. It uses sinks to send logs to text files, databases, log management solutions, or another target. Serilog has a clean API and is easy to set up.

It implements great support from writing logs as JSON by using the built-in JSON formatter.

Log4net

The Apache Foundation’s log4net is the .NET equivalent to log4j, one of the first logging frameworks. Also, log4net is flexible in where the logging information is stored. Moreover, log4net helps program output log statements to various log targets. It supports multiple frameworks and can output to multiple logging targets.

Fortunately, log4Net enables logging at runtime without modifying the code.

Nspring

Nspring is a flexible .NET logging library designed to support the processing of diagnostic messages with any .NET languages. Nspring supports multiple targets ranging from a text file to writing to a remote database. It’s easy to set up. Although it requires lots of code to get it started.

.NET Logging API

Furthermore, .NET provides a logging API designed to simplify logging implementation in your .NET project. In my opinion, it’s the most important building block for implementing logging in .NET. Moreover, the logging API allows you to add the aforementioned libraries or other third-parties by calling the required extension method in Startup.cs. See here for more information.

Using the logging API, the logs will be written to the root directory of your app.

Best Practices

  1. Logs should include contextual information. For example, indications of why the application failed. Furthermore, the logs should include information such as when the failure occurred, where in the code the application failed, and what the system was doing when the failure occurred. This information will help you to better investigate and resolve an event.
  2. Search and filter using context. Logs should be meaningful to the humans who need to read the raw data and also be in formats capable of being parsed by machines. Make log information meaningful by adding extra context information to the log. User-friendly and readable logs can help you find desired information. This prevents confusion.

Ensure log messages aren’t dependent on a previous message. They may be logged separately after filtering is applied if they’re not in the same category. Logs should be set

  1. to capture enough information. Logging at too high a level doesn’t capture enough information. On the other hand, logging too low captures enormous amounts of information with several details hiding the important data, thus making it hard to find an event in the resulting signal.
  2. Secure logs by encrypting and proactively assigning and managing permissions for individuals who need to access the log files. Also, make sure these log files cannot be modified to hide malicious activities to ensure their integrity is maintained. Take measures to confirm logs don’t contain sensitive and private clear text data.
  3. Logs should convert similar elements to the same format (e.g., dates).
  4. Categorize logs based on their level of seriousness. Use proper logging categories to allow the classification of log messages. Use log levels associated with each message.

The Best Software for Logging in .NET

Finally, with the framework and best practices under our belt, let’s take a look at some of the best software for .NET.

1. Papertrail

Papertrail
© 2020 SolarWinds Worldwide, LLC. All rights reserved.

Firstly, SolarWinds® Papertrail is a web application monitoring platform designed to help developers diagnose, fix, and optimize their code’s performance. It logs messages for servers, cloud services, and apps. It aggregates app logs, text log files, and Syslog in one place. This provides sufficient contextual information, allowing you to effectively monitor logs.

Interestingly, Papertrail uses log levels to filter logs and suppress some logging statements. It enriches logs with more context by formatting fields, like the date, for easy understanding. Luckily, it ensures logs are secure by implanting encryption and access control protocols.

Moreover, log categories categorize logs based on their severity for easy filtering and searching. It also provides an intuitive web-based event viewer showing events or logs in real time to uncover insights quickly by using the live tail feature.

Similarly, the advanced searching feature in Papertrail significantly reduces troubleshooting time. Specifically, by using regular expressions and context to search through log data and spot issues faster.

2. Logstash

Logstash
© 2020. Elasticsearch B.V. All Rights Reserved

Another tool is Logstash. It’s a free and open server-side data processing pipeline. Logstash dynamically ingests data of various formats and sizes from multiple sources. It transforms the log data. Then it sends it to the preferred destination. Logstash includes loads of plugins and out-of-the-box connections that can be used to enrich data and thus provide context.

The main aspect of Logstash is its ability to change data on the fly. In other words, you can make changes while processes are in action. Allowing you to log efficiently.

Logstash offers features for secure communication. You can communicate with external systems safely as all plugins support authentication and encryption over HTTP connections.

Above all, configuration of Logstash is simple. You have the option to configure by using the GUI or through the configuration files.

Conclusion

Effective logging is crucial to debugging during the development and production of an application. A good logging infrastructure is vital for system administrators to understand the state of every system’s components and prevent errors, threats, and other inconveniences to each component.

.NET logging provides context-rich logs with sufficient information for troubleshooting, and the ability to be easily parsed by other tools. The right log message can be the key to unlocking the trickiest software issues. To get the best value out of your logging, follow logging best practices, and use a log management tool. Tools like Papertrail, for example, can index all fields and enable powerful analytic and searching capabilities, boosting your .NET logging capabilities.

This post was written by Daniel de Oliveira. Daniel is a full stack developer and analyst, experienced with Java, Angular, and Flutter.

Related Posts