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

Add logs to unit tests #15130

Closed
wants to merge 6 commits into from
Closed

Add logs to unit tests #15130

wants to merge 6 commits into from

Conversation

hyzx86
Copy link
Contributor

@hyzx86 hyzx86 commented Jan 19, 2024

Error messages cannot be obtained when executing network request tests.
To facilitate the analysis of error messages in unit tests, the file logging function is added to unit tests

relate : #14347
image

Summary by CodeRabbit

  • New Features

    • Introduced NLog logging configuration for enhanced logging capabilities in tests.
  • Refactor

    • Updated project configurations to optimize resource file handling.
  • Tests

    • Integrated NLog into the testing framework to improve logging during tests.

@hyzx86 hyzx86 changed the title add logs to unit tests Add logs to unit tests Jan 19, 2024
@hyzx86 hyzx86 mentioned this pull request Jan 19, 2024
3 tasks
{
ext.RegisterLayoutRenderer<TenantLayoutRenderer>("orchard-tenant-name");
});
services.AddLogging(loggingBuilder =>
Copy link
Member

Choose a reason for hiding this comment

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

Why not to use UseNLogHost()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forgot that... 🤣

Copy link
Member

Choose a reason for hiding this comment

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

I think the whole PR will be simplified :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are correct, the PR has been updated

@@ -24,6 +23,7 @@
<None Remove="Modules\OrchardCore.OpenId\RecipeFiles\app-recipe2.json" />
<None Remove="Modules\OrchardCore.OpenId\RecipeFiles\app-recipe3.json" />
<None Remove="Modules\OrchardCore.OpenId\RecipeFiles\scope-recipe.json" />
<None Remove="NLog.config" />
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Log templates are different, such as paths

     <target xsi:type="File" name="file"
             fileName="${var:configDir}/App_Data/logs/${orchard-tenant-name}/orchard-log-${shortdate}.log"
             layout="${longdate}|${orchard-tenant-name}|${aspnet-traceidentifier}|${event-properties:item=EventId}|${logger}|${uppercase:${level}}|${message} ${exception:format=ToString,StackTrace}"
     />

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you don't split the file, it seems that you can only write to a single tenant's log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You were right. But I was wondering, is there any way to add the test name to the log layout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need many changes here, please look into https://github.com/OrchardCMS/OrchardCore/blob/main/src/OrchardCore.Cms.Web/OrchardCore.Cms.Web.csproj

Sometimes we need to use a more detailed log level to output debug logs, We may need two different logging levels

@MikeAlhayek
Copy link
Member

Why do we need to log in tests? When running test, you get enough info from failed test in VS.

@Skrypt
Copy link
Contributor

Skrypt commented Mar 23, 2024

To log when developping them

@hyzx86
Copy link
Contributor Author

hyzx86 commented Mar 23, 2024

Why do we need to log in tests? When running test, you get enough info from failed test in VS.

Sometimes we need to use a more detailed log level to output debug logs

@hyzx86
Copy link
Contributor Author

hyzx86 commented Mar 23, 2024

As for whether to output logs, I wonder if these logs should only be output in debug mode, or output using other commands, such as dotnet test -p:logger=false when starting the test program

@hyzx86
Copy link
Contributor Author

hyzx86 commented Apr 16, 2024

It doesn't seem necessary anymore. Let's close it

@hyzx86 hyzx86 closed this Apr 16, 2024
@hyzx86
Copy link
Contributor Author

hyzx86 commented Apr 29, 2024

@MikeAlhayek
I remember why I want to enable this feature.
This is because some errors in log files are not reported to the console, and the error messages in the console are not related to the actual cause of the error

Let's say that when we install a module that will automatically perform the recipe migration, if the recipe fails, it will write a log to a log file, but it will not be displayed in the test console

@Piedone
Copy link
Member

Piedone commented Apr 29, 2024

So, then, we need logging to the test console, by redirecting the logs to xUnit's ITestOutputHelper somehow.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Apr 29, 2024

Yes, but the recipe execution error is not thrown to the UI, which could be a BUG as well

@hyzx86
Copy link
Contributor Author

hyzx86 commented May 1, 2024

The same issue is also found in the IUserEventHandler handler, where an exception is thrown that doesn't prevent the test from stopping, it just logs it

@hyzx86 hyzx86 reopened this May 1, 2024
Copy link
Contributor

coderabbitai bot commented May 1, 2024

Walkthrough

Walkthrough

The updates involve integrating NLog for enhanced logging capabilities in the OrchardCore.Tests project. These changes include the addition of NLog configuration settings, modification in the project file to adjust resource handling, and the integration of NLog in the MvcTestFixture class for better logging during testing.

Changes

File Path Change Summary
.../MvcTestFixture.cs Added NLog usage and host configuration.
NLog.config Introduced a new configuration file for NLog settings, specifying targets and rules for logging.
.../OrchardCore.Tests.csproj Updated project settings to adjust how NLog.config is handled, removing old references and adding new settings for content management.

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 7bff0b9 and 8c68ad0.
Files selected for processing (3)
  • test/OrchardCore.Tests/Apis/Context/MvcTestFixture.cs (2 hunks)
  • test/OrchardCore.Tests/NLog.config (1 hunks)
  • test/OrchardCore.Tests/OrchardCore.Tests.csproj (3 hunks)
Files skipped from review due to trivial changes (1)
  • test/OrchardCore.Tests/OrchardCore.Tests.csproj

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

@@ -23,6 +25,7 @@ protected override IWebHostBuilder CreateWebHostBuilder()

protected override IHostBuilder CreateHostBuilder()
=> Host.CreateDefaultBuilder()
.UseNLogHost()
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using FakeLogger for unit testing instead of NLog.

Based on the discussion in the PR comments, FakeLogger might be more suitable for unit tests as it is designed specifically for testing scenarios and avoids the overhead of file-based logging. This approach could enhance test performance and manageability.

Comment on lines +1 to +32
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="App_Data/logs/internal-nlog.txt">

<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="OrchardCore.Logging.NLog"/>
</extensions>

<targets>
<!-- file target -->
<target xsi:type="File" name="file"
fileName="${var:configDir}/App_Data/logs/${orchard-tenant-name}/orchard-log-${shortdate}.log"
layout="${longdate}|${orchard-tenant-name}|${aspnet-traceidentifier}|${event-properties:item=EventId}|${logger}|${uppercase:${level}}|${message} ${exception:format=ToString,StackTrace}"
/>

<!-- console target -->
<target xsi:type="Console" name="console" />

</targets>

<rules>
<!-- all warnings and above go to the file target -->
<logger name="*" minlevel="Warn" writeTo="file" />

<!-- the hosting lifetime events go to the console and file targets -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="file, console" />
</rules>
</nlog>
Copy link
Contributor

Choose a reason for hiding this comment

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

Review the logging strategy for unit tests.

While the NLog configuration is correctly set up, reconsider the use of file and console logging in a test environment. Based on the PR discussions, using FakeLogger could be more appropriate for unit tests, focusing on in-memory logging which is faster and more suitable for test scenarios.

@hyzx86 hyzx86 closed this May 4, 2024
@hyzx86 hyzx86 deleted the testlog branch June 13, 2024 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants