Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tomek-i
Copy link
Member

@tomek-i tomek-i commented Nov 7, 2024

  1. What triggered this change? (PBI link, Email Subject, conversation + reason, etc)

✏️BizCover Client Work
SSWConsulting/SSW.SAW.General#8

  1. What was changed?

✏️ Added new rule

  1. Did you do pair or mob programming (list names)?

✏️ 🥷🏻

@github-actions github-actions bot added Age: 🥚 - New About 2 hours old Age: 🐣 - Young About 4 hours old Age: 🐥 - Adolescent About 8 hours old Age: 🐤 - Mature About 16 hours old Age: 🐓 - Old About 32 hours old and removed Age: 🥚 - New About 2 hours old Age: 🐣 - Young About 4 hours old Age: 🐥 - Adolescent About 8 hours old Age: 🐤 - Mature About 16 hours old labels Nov 8, 2024
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### Verifying Log Levels
### Verifying Log Levels

Assert.Contains(logEntries, log => log.LogLevel == LogLevel.Error);
```

#### Verifying Log Event IDs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### 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
Copy link
Member

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

@github-actions github-actions bot added Age: 🍗 - Ancient About 64 hours old 🔥 Merge Debt This PR contains merge debt, see https://www.ssw.com.au/rules/merge-debt/ and removed Age: 🐓 - Old About 32 hours old labels Nov 9, 2024
Copy link
Contributor

github-actions bot commented Nov 9, 2024

Howzit @tomek-i,

This PR has been here a while.

Did you know you should avoid merge debt?

  1. Please action (e.g. get a review) and merge or close

Thanks!

@github-actions github-actions bot added Age: 🦖 - Extinct About 128 hours old and removed Age: 🍗 - Ancient About 64 hours old labels Nov 10, 2024
Copy link
Member

@danielmackay danielmackay left a 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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Age: 🦖 - Extinct About 128 hours old 🔥 Merge Debt This PR contains merge debt, see https://www.ssw.com.au/rules/merge-debt/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants