-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add logs to unit tests #15130
Conversation
{ | ||
ext.RegisterLayoutRenderer<TenantLayoutRenderer>("orchard-tenant-name"); | ||
}); | ||
services.AddLogging(loggingBuilder => |
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.
Why not to use UseNLogHost()
?
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.
I forgot that... 🤣
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.
I think the whole PR will be simplified :)
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.
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" /> |
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.
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
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.
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}"
/>
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.
if you don't split the file, it seems that you can only write to a single tenant's log
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.
You were right. But I was wondering, is there any way to add the test name to the log layout
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.
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
Why do we need to log in tests? When running test, you get enough info from failed test in VS. |
To log when developping them |
Sometimes we need to use a more detailed log level to output debug logs |
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 |
It doesn't seem necessary anymore. Let's close it |
@MikeAlhayek 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 |
So, then, we need logging to the test console, by redirecting the logs to xUnit's |
Yes, but the recipe execution error is not thrown to the UI, which could be a BUG as well |
The same issue is also found in the |
WalkthroughWalkthroughThe 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
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (3)
Files skipped from review due to trivial changes (1)
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 2
@@ -23,6 +25,7 @@ protected override IWebHostBuilder CreateWebHostBuilder() | |||
|
|||
protected override IHostBuilder CreateHostBuilder() | |||
=> Host.CreateDefaultBuilder() | |||
.UseNLogHost() |
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.
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.
<?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> |
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.
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.
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
Summary by CodeRabbit
New Features
Refactor
Tests