-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Examples
See these tutorials is for ASP.NET Core:
- For ASP.NET Core 5, check Getting started with ASP.NET Core 5
- For ASP.NET Core 3, check Getting started with ASP.NET Core 3
- For ASP.NET Core 2, check Getting started with ASP.NET Core 2
- For ASP.NET Core 1? Go here!
Ryan Farley has a great article on how to use NLog with Growl for Windows
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
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json