GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Examples

Examples

Jump to bottom Edit New page
Rolf Kristensen edited this page Aug 8, 2026 · 21 revisions

Using NLog as .NET Core and ASP.NET Core logging provider

  • Getting started for .NET Core Console application
  • Getting started for ASP.NET Core

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.

See also Configure from code

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!

- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config

Home

  • Getting started
    • NLog Introduction
    • Example - ASP.NET Core
    • Example - .NET Core Console
  • How to use structured logging
  • Troubleshooting
    • Internal Log
  • FAQ
  • Articles about NLog

Configuration

  • All targets, layouts and layout renderers
    Popular:
    • File-target
    • ColoredConsole-target
    • Database-target
    • Mail-target
  • Using NLog with NLog.config
  • Using NLog with appsettings.json

Programmatic Configuration

  • Configure from code
  • Sandcastle documentation

Advanced

  • Add LogEvent Context details
  • Filtering log messages
  • Optimize Logging Performance
  • NLog properties with Microsoft Extension Logging
  • NLog GetCurrentClassLogger and Microsoft ILogger
  • Using Time Sources
  • Installing targets
  • Visual Studio support
  • Who is using NLog
  • Advanced Examples

Extending NLog

  • Write custom extensions
  • How to write a custom target
  • How to write a custom layout renderer
  • Writing custom conditions
  • Properties constraints for custom extensions
  • Register your custom component

Clone this wiki locally

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.