-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do you use logging fakes #9565
base: main
Are you sure you want to change the base?
Do you use logging fakes #9565
Conversation
guid: dc76970f-deea-4e92-9d55-731e83bee7b5 | ||
--- | ||
|
||
Unit tests are an essential part of ensuring that your .NET applications work correctly. However, when it comes to testing logging, things can get tricky. You don't want to rely on actual logging infrastructure like file writes or external logging systems during tests, as this could lead to unnecessary complexity and slow down your test suite. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit tests are an essential part of ensuring that your .NET applications work correctly. However, when it comes to testing logging, things can get tricky. You don't want to rely on actual logging infrastructure like file writes or external logging systems during tests, as this could lead to unnecessary complexity and slow down your test suite. | |
Unit tests are an essential part of ensuring .NET applications work correctly. However, when it comes to testing logging, things can get tricky. You don't want to rely on actual logging infrastructure like file writes or external logging systems during tests, as this could lead to unnecessary complexity and slow down your test suite. |
|
||
Microsoft provides a simple solution with `FakeLogger`. This logger allows you to capture and verify logging calls in unit tests without needing to integrate with a real logging framework. | ||
|
||
By incorporating `FakeLogger` into your tests, you can ensure that your logging functionality is correct and meets your application's needs, all while maintaining fast and isolated tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By incorporating `FakeLogger` into your tests, you can ensure that your logging functionality is correct and meets your application's needs, all while maintaining fast and isolated tests. | |
By incorporating `FakeLogger` into tests, you can ensure that the logging functionality is correct and meets your application's needs, all while maintaining fast and isolated tests. |
|
||
The FakeLogger is a mock logger provided by Microsoft for unit testing. It is part of the Microsoft.Extensions.Logging namespace and is used primarily in tests to verify that logging actions occur during the execution of the application, without sending log output to external destinations like the console or log files. Instead, it "fakes" the log output, allowing assertions to be made on the log entries. | ||
|
||
The advantage of using `FakeLogger` is that it behaves like a real `ILogger<T>`, but it stores the logs in memory, making them available for examination during your tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The advantage of using `FakeLogger` is that it behaves like a real `ILogger<T>`, but it stores the logs in memory, making them available for examination during your tests. | |
The advantage of using `FakeLogger` is that it behaves like a real `ILogger<T>`, but it stores the logs in memory, making them available for examination during tests. |
|
||
## Benefits of using FakeLogger | ||
|
||
- **No External Dependencies**: Since `FakeLogger` does not actually write logs anywhere (such as to disk or a remote server), it keeps your unit tests isolated and fast. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- **No External Dependencies**: Since `FakeLogger` does not actually write logs anywhere (such as to disk or a remote server), it keeps your unit tests isolated and fast. | |
- **No External Dependencies** - Since `FakeLogger` does not actually write logs anywhere (such as to disk or a remote server), it keeps unit tests isolated and fast |
## Benefits of using FakeLogger | ||
|
||
- **No External Dependencies**: Since `FakeLogger` does not actually write logs anywhere (such as to disk or a remote server), it keeps your unit tests isolated and fast. | ||
- **Verify Log Entries**: You can assert that certain log messages were generated during the execution of specific methods, helping to verify that your logging statements are firing correctly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- **Verify Log Entries**: You can assert that certain log messages were generated during the execution of specific methods, helping to verify that your logging statements are firing correctly. | |
- **Verify Log Entries** - You can assert that certain log messages were generated during the execution of specific methods, helping to verify that logging statements are firing correctly |
|
||
## How to use FakeLogger | ||
|
||
To get started with `FakeLogger`, you need to install the necessary package: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get started with `FakeLogger`, you need to install the necessary package: | |
To get started with `FakeLogger`, you need to install the necessary package. |
|
||
To get started with `FakeLogger`, you need to install the necessary package: | ||
|
||
Once you have the package installed, you can begin using the FakeLogger in your unit tests. Here's how to use it: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you have the package installed, you can begin using the FakeLogger in your unit tests. Here's how to use it: | |
Then you can begin using the FakeLogger in unit tests. Here's how to use it: |
|
||
While the basic usage of FakeLogger is straightforward, you can also use it to verify log levels, event IDs, and other log-related data. | ||
|
||
#### Verifying Log Levels |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### Verifying Log Levels | |
### Verifying Log Levels |
Assert.Contains(logEntries, log => log.LogLevel == LogLevel.Error); | ||
``` | ||
|
||
#### Verifying Log Event IDs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### Verifying Log Event IDs | |
### Verifying Log Event IDs |
seoDescription: Learn how to use Microsoft's `FakeLogger` in unit tests to verify logging behavior in .NET applications. This guide shows you how to capture log entries, check log levels, and ensure your logging works as expected during testing. | ||
type: rule | ||
title: Do you use Logging Fakes? | ||
uri: do-you-use-logging-fakes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ho @tomek-i
- 1. Please avoid the "do-you-" on URLs, making it shorter and friendlier
Howzit @tomek-i, This PR has been here a while. Did you know you should avoid merge debt?
Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment
seoDescription: Learn how to use Microsoft's `FakeLogger` in unit tests to verify logging behavior in .NET applications. This guide shows you how to capture log entries, check log levels, and ensure your logging works as expected during testing. | ||
type: rule | ||
title: Do you use Logging Fakes? | ||
uri: do-you-use-logging-fakes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rule needs to be linked to one or more categories
✏️BizCover Client Work
SSWConsulting/SSW.SAW.General#8
✏️ Added new rule
✏️ 🥷🏻