.NET Logging Guide:
Logging Mistakes and Security Best Practices

Arfan Sharif - January 20, 2023

Integrating more advanced logging into your application becomes more important when prioritizing security and avoiding logging mistakes. Poorly designed logs can be difficult to read and understand, making it tough to identify and troubleshoot problems. Not organizing or formatting your application logs complicates the task of searching and filtering for information, potentially delaying the resolution of issues.

In this part of our .NET logging guide series, we relate the common logging mistakes made in .NET and how to avoid them. We also cover the security considerations for logging in .NET. Overall, avoiding logging mistakes and adhering to security best practices are two crucial ways to ensure application logs are effectively tracking and monitoring the performance of your system.

Learn More

Explore the complete .NET Logging Guide series:

Logging Mistakes in .NET

Logging mistakes can lead to increased software engineering costs, adverse effects on application performance and issues with security and compliance. Let’s dive into some common mistakes in .NET that cause challenging and hard-to-manage logging issues.

Memory allocation

Memory allocation occurs when a computer assigns some of its memory to a software process. At the beginning of a new software process, the runtime system dedicates part of the computer’s memory, called the managed heap, for the process to use. However, if a process inefficiently creates an enormous number of unnecessary objects or uses excessive logging methods, the managed heap becomes cluttered. The application then runs on little to no free memory, causing it to operate slowly or even crash.

Blocked logging

Application logging consumes processing time and resources and may cause the application to run slowly. When the main thread is in a block state due to a logging operation, further application execution halts until the logging operation finishes. This state causes the application to stop working, which would create a bad user experience.

Unnecessary logging calls

Making unnecessary logging calls can lead to an increased need for storage space, and this can become expensive. Additionally, valuable information is difficult to locate amidst the noise, as log query results contain vague data mixed with important log information.

Ignoring the templating feature

Templating is a feature of semantic logging. Semantic logging uses a consistent, predetermined message format for application logs so that the logs are captured as searchable and parsable records. Ignoring the templating feature means missing out on many performance benefits, like memory optimization and generically typed log data parameters. The alternative to templating is to use unstructured logs, which can make it harder to query log data.

Retaining logs indefinitely

Log retention is a crucial part of compliance. Different regions and industries have policies that govern how and how long log data should be stored. Indefinitely retaining application logs may be a compliance breach against these policies. In addition, storing old application logs requires investing in more storage space, which translates to higher application costs.

How To Avoid Logging Mistakes

To effectively track and monitor the performance of your application or system, it is critical to adopt measures to avoid the mistakes listed above. The behavior and performance of your system, including any errors or issues that may occur, can be gleaned from well-designed logs. Below is a list of practices that can help you avoid the common mistakes.

Implement non-blocking logging

Perform logging on a different application thread, independent from the main application thread. Using a separate application thread ensures that the main application thread remains unblocked and that it can execute other tasks without being slowed down by the logging functions.

Use log levels

Log levels are vital because they allow you to control and filter the information in your logs. Different log levels correspond to various levels of severity. By setting a log level, you can specify which events to log and which to ignore. Log levels can help you focus on the most crucial information while also preventing storage overhead and memory allocation issues.

Use the templating feature

Combining semantic logging with custom extension methods makes log data parameters generically typed and thus not boxed. This saves memory on the managed heap when the logger is not logging.

Adopting templates also helps to standardize logging by having a precise, structured logging method. This lowers the mental work associated with translating logs to ascertain what transpired in systems.

Implement a log retention policy

A log retention policy is a set of rules that define how long an application should retain log data before deleting it. A log retention policy can provide multiple benefits for organizations, including adhering to compliance requirements, conserving storage space, enhancing performance, boosting data analysis and protecting privacy.

Security Best Practices for .NET Logging

Security considerations further optimize your logging in .NET applications. Effective security policies allow organizations to control access to their log data and protect the data from misuse, theft, and loss. These policies help to prevent unintentional data leaks and ensure compliance with security regulations.

Prioritizing security in logging improves the quality of applications and protects the organization’s reputation. To do this, keep in mind the following security considerations.

Avoid logging personally identifiable information

Personally identifiable information (PII) is any information that identifies an individual, such as a user’s name, email address or password. To safeguard the security and privacy of their users, applications should refrain from logging PII. Logging PII data can put organizations in legal danger because many governments have laws that control how PII is collected, stored and used.

Any organization that violates these PII laws may incur heavy fines and suffer reputational harm. Alternate methods for dealing with PII include using anonymous identifiers or hashed versions of PII. This strategy will reduce the risks and liabilities for a company while also assisting in protecting the security and privacy of individuals.

Use access control lists to protect log files

Access control lists (ACLs) are a security precaution that controls access to log files. ACLs define which users or groups have access to particular resources, like log files, and what operations can occur on those resources. For example, an ACL might permit some users to read log files while prohibiting them from editing or deleting them. You can ensure that only authorized users can access log files by using ACLs to protect them. This protection can prevent unauthorized access to sensitive information and guarantee the integrity of your log files.

Choose log file extensions that web requests don’t support

One option for securing logs is choosing a file extension for application logs that web requests don’t support. This method requires using a file extension that is not typically associated with web-based content. For example, you could use a file extension such as .log for text-based log files, as doing so helps to prevent external API calls from fetching log data.

Use absolute paths for log file location

An absolute path is the full, specific location of a file or directory on a computer’s file system. Using an absolute file path can help prevent external access to the log file because it requires knowledge of the exact location of the file rather than simply being able to navigate to the file from a known starting point.

Run security compliance checks

An important step in software development is performing security compliance checks on applications before releasing them to the public. Before making an application available to the general public, these checks confirm that it is safe and compliant with accepted security protocols. Beyond providing application security, many compliance validation tools can check that your application has not logged sensitive data like PII or service credentials. These checks can also validate that software developers are not using vulnerable logging libraries.

Conclusion

This article explored logging mistakes in .NET and the measures to adopt for avoiding them. We also discussed the different ways to implement security in .NET logging. By following these best practices and adhering to security recommendations, your organization can ensure that your application logs are secure, well-organized, easily searchable and able to provide the information you need when you need it.

Organizations may also have specific needs when it comes to logging destinations. Beyond log files, debuggers and application consoles, there will be instances where organizations want to log to more structured log destinations. In the next part of this series, we will explore the use of custom log providers for logging to other destinations beyond the .NET defaults.

Log your data with CrowdStrike Falcon Next-Gen SIEM

Elevate your cybersecurity with the CrowdStrike Falcon® platform, the premier AI-native platform for SIEM and log management. Experience security logging at a petabyte scale, choosing between cloud-native or self-hosted deployment options. Log your data with a powerful, index-free architecture, without bottlenecks, allowing threat hunting with over 1 PB of data ingestion per day. Ensure real-time search capabilities to outpace adversaries, achieving sub-second latency for complex queries. Benefit from 360-degree visibility, consolidating data to break down silos and enabling security, IT, and DevOps teams to hunt threats, monitor performance, and ensure compliance seamlessly across 3 billion events in less than 1 second.

Schedule Falcon Next-Gen SIEM Demo

GET TO KNOW THE AUTHOR

Arfan Sharif is a product marketing lead for the Observability portfolio at CrowdStrike. He has over 15 years experience driving Log Management, ITOps, Observability, Security and CX solutions for companies such as Splunk, Genesys and Quest Software. Arfan graduated in Computer Science at Bucks and Chilterns University and has a career spanning across Product Marketing and Sales Engineering.