Logging to file in asp net core using nlog



Logging to file in asp net core using nlog

Logging to file in asp net core using nlog

In this video we will discuss how to log to a file in ASP.NET Core using NLog.

Text version of the video
https://csharp-video-tutorials.blogspot.com/2019/05/logging-to-file-in-aspnet-core-using.html

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Slides
https://csharp-video-tutorials.blogspot.com/2019/05/logging-to-file-in-aspnet-core-using_30.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

ASP.NET Core supports several third-party logging providers like the following
NLog
Serilog
elmah
Sentry
JSNLog

If we know how to work with one of the third-party logging providers, working with the other’s is similar.

To use NLog in ASP.NET Core

Step 1 : Install NLog.Web.AspNetCore nuget package

Once the NLog package is iinstalled, you will see the PackageReference included in the .csproj file

Step 2 : Create nlog.config file

Create nlog.config file in the root of your project. Please use the configuration available at the following link.
https://csharp-video-tutorials.blogspot.com/2019/05/logging-to-file-in-aspnet-core-using.html

To learn more about the nlog.config file please refer to the following github wiki page
https://github.com/NLog/NLog/wiki/Configuration-file

Step 3 : Enable copy to bin folder

Right click on nlog.config file in the Solution Explorer and select Properties. In the Properties window set
Copy to Output Directory = Copy if newer

Step 4 : Enable NLog as one of the Logging Provider

In addition to using the default logging providers (i.e Console, Debug & EventSource), we also added NLog using the extension method AddNLog(). This method is in NLog.Extensions.Logging namespace.

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =]
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =]
{
logging.AddConfiguration(hostingContext.Configuration.GetSection(“Logging”));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
// Enable NLog as one of the Logging Provider
logging.AddNLog();
})
.UseStartup[Startup]();
}

If you want only NLog as the logging provider, clear all the logging providers and then add NLog.

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =]
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =]
{
// Remove all the default logging providers
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection(“Logging”));
// Add NLog as the Logging Provider
logging.AddNLog();
})
.UseStartup[Startup]();
}

Next video : Control what is logged using the LogLevel configuration setting.

Comments are closed.