For building good and reliable software, logging is essential. But not every type of data should be logged. With all the GDRP regulations it’s important to take care of your logging.
By default you should not expose data in your logging. But sometimes it can be handy to see your objects in the logs. This post will discuss methods and show you how to mask data in Azure Application Insights.
Log masking in .NET 8
There are serveral ways to do Log masking in .NET since version 6. Now let’s look what is available for us for .NET 8 and higher.
Logging source generation
By using Source generation for logging you will be able to mask data in your logs. Let’s take a look at how.
Step 1: Add Microsoft.Extensions.Logging
First, you need to add the Microsoft.Extensions.Logging
package to your project. You can do this by running the following command in your project directory:
|
|
Step 2: Use the LogPropertyIgnore in Your Models
Now you can use the [LogPropertyIgnore]
in your models to mark properties that should be not be logged.
|
|
Step 3: Logging or Sending Data
When you log or send instances of MyModel
, the SensitiveData
property will be masked in the JSON output.
|
|
See a codesample on GitHub: Log Masking in Application Insights.
Log masking using Custom own made code
While researching for log masking in .NET core i stumbled on this older article: https://dev.to/krusty93/hide-sensitive-data-in-azure-application-insights-logs-of-our-aspnet-core-apis-4ji7.
This article describes how to mask data in your logs. The article describes how to use a custom JsonConverter
to mask sensitive data in your logs. This approach allows you to define which properties should be masked and how they should be masked. You can use this approach to mask sensitive data in your logs while still using the built-in JSON serialization features of .NET.
It feels cheaty to do this. But it is a possible path to mask your logs. Also as you can see in the Performance section of this blog, you should be carefull with this approach.
Using the Redaction package
The Redaction
package is a simple and effective way to mask sensitive data in your logs. This package allows you to define a list of properties that should be redacted when logging objects. You can install the package using the following command:
Step 1 installing the package
|
|
Step 2 Create data classification
Create a data classification that defines the properties that should be redacted. You can do this by creating a class that implements the IDataClassification
interface:
|
|
Step 3 Use the data classification
Now you can use the PrivateDataAttribute
attribute to mark properties that should be redacted:
|
|
Step 4 Configure the Redaction Options
You can add custom Redactors, and apply them to different classifications of data.
|
|
You can configure the redaction options in your Startup.cs
file:
|
|
Discussion
Personally i am most charmed by the source generation method of logging. The logging is clean and simple enough to use. The redaction package is a more complex way of masking your logs, but has more capabilities to mask and classify different types of data.
Log masking in .NET Framework 4.8
So you are still using .NET Framework 4.8? No worries, you can still mask data in your logs. You are entitled to use build your own TelemetryInitializer. This way you can mask data in your logs.
Step 1: Create a TelemetryInitializer
You need to add properties yourself to the request telemetry. You can do this as seen in the code below. This code does not support lists and such, but suit yourself for that. You can even make it recursive if you would like to log nested objects.
|
|
Step 2: Use the LogObject
|
|
Conclusion
By creating a custom attribute and a JSON converter, you can effectively mask sensitive properties during serialization in your .NET 8 application. This approach allows you to maintain clean and secure logging practices while still using the built-in JSON serialization features of .NET. Always ensure that you are compliant with data protection regulations when handling sensitive data.
References
- https://andrewlock.net/behind-logproperties-and-the-new-telemetry-logging-source-generator/
- https://andrewlock.net/exploring-dotnet-6-part-8-improving-logging-performance-with-source-generators/
- Log Masking
- https://learn.microsoft.com/en-us/training/modules/dotnet-compliance-cloud-native-applications/
- https://gist.github.com/joperezr/f5f022bcb4d0ce8f077e40e1f77239c8#file-redaction-md
- dev.to - Hide sensitive data in Azure Application Insights logs of our ASP.NET (Core) APIs