Examples

James Sconfitto edited this page Sep 26, 2017 · 12 revisions

Using NLog with GMail

In order to use Mail target with GMail, you need to use the following server configuration:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="gmail" xsi:type="Mail"
            smtpServer="smtp.gmail.com"
            smtpPort="587"
            smtpAuthentication="Basic"
            smtpUserName="user@gmail.com"
            smtpPassword="password"
            enableSsl="true"
            from="emailaddress@gmail.com"
            to="recipient@example.com"
            cc="alice@example.com;bob@example.com;charlie@example.com"
          />
  </targets>

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

Using NLog with Growl for Windows

Ryan Farley has a great article on how to use NLog with Growl for Windows

Changing log file name at runtime

using NLog.Targets;
...
FileTarget target = LogManager.Configuration.FindTargetByName<FileTarget>(targetName);
target.FileName = filename;

Note that it is often easier to create the file name with a combination of Layout Renderers.

Using NLog with Autofac - ASP.NET Web API 2

This example is using a nuget package Autofac.Extras.Nlog that takes care of creating the Autofac module needed to wire up the dependency in the Web API.

First, add an nlog section to the configSections in the Web.config:

  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

Then, in the Global.asax.cs (or Startup.cs, depending where Autofac is configured) register the module provided by the Autofac.Extras.Nlog nuget package:

  var builder = new ContainerBuilder();
   ...

  builder.RegisterModule<NLogModule>();

Nothing extra is needed to configure the NLog targets.

This example uses constructor injection but Autofac.Extras.Nlog supports property and service locator injection as well.

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Autofac.Extras.NLog;
using Microsoft.AspNet.Identity;

    public class UserService: IUserService
    {
        private readonly IUserStore _userStore;
        private readonly ILogger _logger;

        public UserService(IUserStore userStore, ILogger logger)
        {
            _userStore = userStore;
            _logger = logger;
        }

        .....
    }

Happy logging!

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.