Update Logging to Structured. #1306
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Found multiple occurrences of CA2254 in the code base.
This is detected when:
Using semantic logging with
Microsoft.Extensions.Logging
offers several advantages over simple string concatenation. Here are some key points, supported by documentation:Performance Efficiency
String concatenation in logging can be inefficient, especially when logging is disabled. The concatenation operation still occurs, consuming resources unnecessarily. Semantic logging, on the other hand, uses structured logging, which avoids this overhead by deferring the creation of the log message until it's actually needed.
Structured Data
Semantic logging allows you to log structured data, which can be easily parsed and analyzed by logging systems. This means you can include properties and context in your logs, making it easier to filter and search through logs based on specific criteria. For example:
This logs the UserId and LoginTime as separate properties, which can be queried independently.
Consistency and Readability
Using semantic logging ensures that log messages are consistent and readable. It separates the message template from the actual values, making it easier to maintain and understand the logs. This is particularly useful in large applications where multiple developers are contributing to the logging.
Enhanced Debugging and Monitoring
Semantic logging integrates well with various logging backends and monitoring tools like Application Insights, Kibana, and Seq. These tools can leverage the structured data to provide more insightful analytics and visualizations, helping in quicker diagnosis and resolution of issues.
Security
String concatenation can introduce security risks, such as injection attacks, if not handled properly. Semantic logging mitigates these risks by treating the message template and values separately, reducing the chances of malicious data being logged.
Flexibility and Extensibility
Microsoft.Extensions.Logging provides a flexible and extensible logging framework that supports multiple logging providers. This means you can easily switch or add new logging providers without changing your logging code2.
Example Comparison
##String Concatenation
Semantic Logging
In summary, semantic logging with
Microsoft.Extensions.Logging
offers significant benefits in terms of performance, structure, readability, security, and integration with monitoring tools, making it a superior choice over simple string concatenation for logging purposes.References