Orchard Core as a NuGet package - getting logging working

I know that logging isn't the most glamorous subject, so I'll keep this short.

Install the logging NuGet package

I'll assume that you have followed my previous post, and that you already have the correct package source set up (if not, the instructions are here). So lets head over to the NuGet package manager, find the package called OrchardCore.Logging.NLog, and install it into the web project.

Those of you paying attention may have noticed that Orchard Core uses the NLog logging framework, whereas Orchard uses log4net. It's worth pointing this out, just in case you were planning on using your existing logging config files from your Orchard projects.

Telling your web app that you want to use Orchard's logging implementation

Back to your Startup.cs file, and the first thing to do is to add another argument to the Configure method. The signature should now look like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

Don't worry about the signature change; ASP.NET Core is smart enough to know what to do here.

The next thing to do is to tell the application to use this method of logging. This is done by adding the line:

app.UseNLogWeb(loggerFactory, env);

My entire Configure method now looks like this:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseNLogWeb(loggerFactory, env);
app.UseModules();
}

Just one more thing to do, and then we stop talking about logging:

Configuration

We now have our logging hooked up, but NLog doesn't know what to log, or where to log it to. I won't go into too much detail, as NLog configuration is pretty well covered elsewhere, but the first thing you need is a NLog.config file at the root of your web project.

Once you've created that file, go ahead and paste this snippet as the contents:

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

<rules>
<logger name="*" minlevel="Debug" writeTo="allfile" />
</rules>
</nlog>

This is a fairly standard configuration file. It creates a target called allfile that logs to a text file on disk, and then configures all loggers to log at Debug level to the allfile logger. Once you've established that the logging is working as expected, you may want to raise the log level to something higher than Debug.

Take a closer look at that configuration, and you might notice that the current tenant's name is includes in the message layout. This possible because the OrchardCore.Logging.NLog assembly takes advantage of an NLog extension point to provide this (which is why we add it in the extensions element in the configuration file).

1 comment

  • ... Posted by Satish Appasani Posted 07/08/2018 01:30 PM

    Since publishing of this article, Orchard team has move the UseNLogWeb() method from ApplicationBuilderExtensions to WebHostBuilderExtensions.

    https://github.com/OrchardCMS/OrchardCore/commit/fae93e558950d7f0eaee14ea134d983f2b1fa416#diff-a182982e3bbd709935f3b4d8bb5ab5f0 has the corresponding commit.

Leave a comment