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

#5 Add Orchard.Logging module adding NLog to pipeline #299

Merged
merged 8 commits into from
Apr 17, 2017

Conversation

jersiovic
Copy link
Contributor

Issue #5 related to this topic was closed but at the end I didn't find lines in Orchard 2 implementing the solution provided there. So, here is PR doing what there is described.

@jersiovic jersiovic force-pushed the #5_pluginARealLogger branch from 53664fb to 31df86e Compare October 31, 2016 11:50
Copy link
Member

@Jetski5822 Jetski5822 left a comment

Choose a reason for hiding this comment

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

Any way to specify the config is in the module rather than under the host?

@Jetski5822
Copy link
Member

Yup, check out https://github.com/NLog/NLog.Extensions.Logging/blob/master/src/NLog.Extensions.Logging/AspNetExtensions.cs

Sent from my iPhone

On 31 Oct 2016, at 18:44, Sergio Navarro notifications@github.com wrote:

Issue #5 related to this topic was closed but at the end I didn't find lines in Orchard 2 implementing the solution provided there. So, here is PR doing what there is described.

You can view, comment on, or merge this pull request online at:

#299

Commit Summary

Add Orchard.Logging module adding NLog to pipeline
File Changes

M Orchard.sln (7)
A src/Orchard.Cms.Web/Modules/Orchard.Logging/Module.txt (10)
A src/Orchard.Cms.Web/Modules/Orchard.Logging/Orchard.Logging.xproj (21)
A src/Orchard.Cms.Web/Modules/Orchard.Logging/Properties/AssemblyInfo.cs (19)
A src/Orchard.Cms.Web/Modules/Orchard.Logging/Startup.cs (30)
A src/Orchard.Cms.Web/Modules/Orchard.Logging/project.json (26)
M src/Orchard.Cms.Web/Modules/Orchard.Setup/Recipes/core.recipe.json (1)
A src/Orchard.Cms.Web/NLog.config (21)
Patch Links:

https://github.com/OrchardCMS/Orchard2/pull/299.patch
https://github.com/OrchardCMS/Orchard2/pull/299.diff

You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@sebastienros
Copy link
Member

Created an issue to talk about it first: #300

@jersiovic jersiovic force-pushed the #5_pluginARealLogger branch from 31df86e to 972abcf Compare November 17, 2016 11:23
@jersiovic
Copy link
Contributor Author

In the mean time I have moved NLog configuration to the Startup.cs of the Orchard.Cms.Web project for this PR. Maybe it is useful while #300 is designed.

@sebastienros
Copy link
Member

Yes, this is better. Following up on the issue.

@sebastienros
Copy link
Member

Is there a way with NLog to get one log file per tenant?

@jersiovic
Copy link
Contributor Author

I don't know an specific way for doing that. In orchard one I passed a parameter to Log4Net with the tenant name.
In NLog sth like that can be done with this https://github.com/NLog/NLog/wiki/Event-Context-Layout-Renderer but maybe it would be more desirable to use one of those renderers, storing in httpcontext or in request the tenant name https://github.com/NLog/NLog/wiki/Layout-Renderers#nlogweb-package-
Next in NLog config you can configure one file per tenant

@agriffard
Copy link
Member

Please, can you resolve the conflicts?

@jersiovic
Copy link
Contributor Author

Sorry I din't read you. ASAP I will do it

@jersiovic jersiovic force-pushed the #5_pluginARealLogger branch from 972abcf to f9d19ca Compare January 11, 2017 23:09
@jersiovic
Copy link
Contributor Author

Rebase done, you can merge it

@agriffard
Copy link
Member

I tested it after retrieving your xkproject branch.
It logged in src\Orchard.Cms.Web\App_Data\Logs\orchard-log-2017-01-20.log

I had multiple Warnings after the setup :
2017-01-20 12:17:07.9323|0|Orchard.Environment.Shell.ShellFeaturesManager|WARN|Additional features need to be enabled.

I created a tenant and tried to generate an error (ex: change the site theme to 'sdf' and reload the homepage).
Is the tenant name supposed to appear somewhere?

Do you have examples of actions creating a log?

Can we have more details about the stack trace, file, line number, ...?

@sebastienros
Copy link
Member

We need the tenant to be rendered, in O1 it's complex to do, how is it with NLog?
I assume the "line number" is rendered with the message of the exception, so I don't think there is anything special to do.

@jersiovic
Copy link
Contributor Author

Sorry I only updated this branch but I didn't know you want I render tenant.
I've just committed changes for showing tenant and stacktrace in the log

@jersiovic
Copy link
Contributor Author

@agriffard said:

Do you have examples of actions creating a log?

Do you mean an Nlog cofig file? Sth like that http://www.peteonsoftware.com/index.php/2008/07/29/nlog/ ?
In general any action you can use can be found in the NLog wiki https://github.com/NLog/NLog/wiki . Specially useful is its Search in documentation link.

Can we have more details about the stack trace, file, line number, ...?

For getting more info related with exceptions, stack trace, file and line you can use these renderers:
https://github.com/NLog/NLog/wiki/Stack-Trace-Layout-Renderer
https://github.com/NLog/NLog/wiki/Exception-layout-renderer
https://github.com/NLog/NLog/wiki/Callsite-Layout-Renderer

@sebastienros
Copy link
Member

Are we logging the RequestId?

@jersiovic
Copy link
Contributor Author

No we are not logging RequestID. Sth like that would be ok? https://github.com/nlog/nlog/wiki/Trace-Activity-Id-Layout-Renderer

@jersiovic
Copy link
Contributor Author

jersiovic commented Feb 2, 2017

Correction: what I mentioned doen't work for Net.Core. The easiest way to get that is to use a custom layout renderer based on a lambda that prints TraceIdentifier:
LayoutRenderer.Register("trace-identifier", (logEvent) => HttpContext.Current.TraceIdentifier);
Is this equivalet to the RequestID you asked, isn't it?
https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer

@sebastienros sebastienros force-pushed the master branch 2 times, most recently from 37f0cda to 081a307 Compare April 6, 2017 02:35
@jersiovic jersiovic force-pushed the #5_pluginARealLogger branch from aa020e1 to 29f5891 Compare April 12, 2017 22:37
@jersiovic
Copy link
Contributor Author

Done.
Now tenant name is stored on httpcontext through a middleware declared on Orchard.Cms.Web project and traceidentifier has been added to nlog.config


public async Task Invoke(HttpContext httpContext)
{
var shellSetting = _runningShellTable.Match(httpContext);
Copy link
Member

Choose a reason for hiding this comment

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

ShellSettings is already added as a Feature to HttpContext

Copy link
Member

Choose a reason for hiding this comment

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

Well but I assume you need a property for the logging ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah ok. Let me check if I can access directly to it

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've removed this class, this middleware is don't needed anymore.

@@ -48,7 +48,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
loggerFactory.AddDebug();
}

app.UseModules();
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.

I explained it on next comment.
This is the implementation of UseModules()

public static IApplicationBuilder UseModules(this IApplicationBuilder app)
        {
            // Ensure the shell tenants are loaded when a request comes in
            // and replaces the current service provider for the tenant's one.
            app.UseMiddleware<ModularTenantContainerMiddleware>();
            app.UseMiddleware<ModularTenantRouterMiddleware>();

            return app;
        }

Problem is I need the new middleware registered just after ModularTenantContainerMiddleware registration. So, I replaced UseModules() per its implementation with the new middleware registered in the position I need.

@@ -48,7 +48,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
loggerFactory.AddDebug();
}

app.UseModules();
app.UseMiddleware<ModularTenantContainerMiddleware>();
app.UseMiddleware<StoreTenantNameAtHttpContextMiddleware>();
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 need the tenant name stored at httpcontext just after the tenant pipeline is initialized to log properly the tenant name on the first request after starting the server. That's why I need to replace app.UseModules() per what it does.
I'm not happy with the solution of not using app.UseModules() .
@sebastienros previously I set it within ModularTenantContainerMiddleware but you told me I should avoid to have logging code specific for a log library within OrchardCore. However thinking it twice to store tenant name in httpcontext for sharing it with other middlewares is not specific code for an specific log library. Don't you prefer the previous solution than this one?

@jersiovic
Copy link
Contributor Author

Done. I've replaced the new middleware per an NLog layout renderer that directly uses ShelSettings in Httpcontext.Features. Now it looks good

@jersiovic
Copy link
Contributor Author

After last commit removing unneeded middleware class IMO this PR is ready to merge

sebastienros
sebastienros previously approved these changes Apr 17, 2017
@sebastienros
Copy link
Member

Looks very nice and clean. We could work on the list and order of things to log, but can be done later when people really care, details.

@@ -41,6 +40,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

app.UseStaticFiles();
loggerFactory.AddConsole(Configuration);
Copy link
Member

Choose a reason for hiding this comment

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

I assume that here we have some configuration for the ConsoleLogger which is different from the NLog one? OR is it possible to used only NLog even for the console? And default the Console to Error, and keep Warning in the log file.

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 are right NLog can be used also for console. I will change that on another PR

@Jetski5822
Copy link
Member

Jetski5822 commented Apr 17, 2017

Talked to @sebastienros - I think this should be in a separate project inside of the OrchardCore folder...

in here: https://github.com/OrchardCMS/Orchard2/tree/master/src/OrchardCore

called Orchard.Logging.NLog

This is not a module, just a separate package.

move, TenantLayoutRenderer, and the setup there.... then in the Startup.cs file, do .UseOrchardNLog.

If you want, I can do it for you? and do a PR to your PR?

Still awesome stuff so far, just think we can reuse better.

@Jetski5822 Jetski5822 dismissed sebastienros’s stale review April 17, 2017 21:26

I want some changes first :)

@Jetski5822 Jetski5822 merged commit eee12aa into OrchardCMS:master Apr 17, 2017
@Jetski5822
Copy link
Member

Changed my mind :) - Lets take this in, and implement the changes as the next step.

Good work @jersiovic ....

@jersiovic
Copy link
Contributor Author

@jetski5832 thank you. Yes I see it is better for reusing it to move that to another project. I will change also this in another PR

@jersiovic jersiovic deleted the #5_pluginARealLogger branch May 13, 2017 21:33
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.

4 participants