Nlog is a useful tool for logging the errors and such information in the Event Log. In here, let us see the process of using Nlog for error logging in .NET Core Web API.
Let's get started.
- Open Visual Studio and create a new project.
- Select ASP.NEt Core Web Application.
- Select API as a template and click the OK button.
Add the Nlog.WindowsEventLog NuGet package.
Let's create an interface having each kind of different methods to store the logs like Debug, Warning, Information, Error etc.
- namespace CoreNLogEventLog
- {
- public interface ILog
- {
- void Information(string message);
- void Warning(string message);
- void Debug(string message);
- void Error(string message);
- }
- }
Implement the above interface in the class and also, get the CurrentClassLogger through LogManager of Nlog, i.e., logger.
Initialize an instance of LogEventInfo with the log level type, loggername, and message.
- using NLog;
- using System;
- using System.Collections.Generic;
- namespace CoreNLogEventLog
- {
- public class LogNLog : ILog
- {
- private static ILogger logger = LogManager.GetCurrentClassLogger();
- public LogNLog()
- {
- }
- public void Debug(string message)
- {
- Logger logger = LogManager.GetLogger("EventLogTarget");
- var logEventInfo = new LogEventInfo(LogLevel.Error, "EventLogMessage", $"{message}, generated at {DateTime.UtcNow}.");
- logger.Log(logEventInfo);
- //LogManager.Shutdown();
- }
- public void Error(string message)
- {
- logger.Error(message);
- }
- public void Information(string message)
- {
- logger.Info(message);
- }
- public void Warning(string message)
- {
- logger.Warn(message);
- }
- }
- }
In the startup.cs, load the configuration for Nlog. For now, give the name like nlog.config and give the root path. In the next step, we will add this file and provide the configuration.
Also, we need to add a singleton service of type Ilog with an implementation type LogNLog.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using NLog;
- namespace CoreNLogEventLog
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- LogManager.LoadConfiguration(String.Concat(Directory.GetCurrentDirectory(), "/nlog.config"));
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- services.AddSingleton<ILog, LogNLog>();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc();
- }
- }
- }

Join the conversation! Your thoughts help the community grow.