In my previous article, we saw how we can log information or errors in Event Log using Nlog in NET Core Web API. Here, let us use NLog for information logging while using RabbitMQ.
Let's start the step-by-step process now.
Create a project
- Open Visual Studio and create a new project.
- Select ASP.NET Core Web Application.
- Select API as the template and click the OK button.

Add Nlog.RabbitMQ.Target the NuGet package to your project.

Let's create an interface having each kind of different method to store the logs, like Debug, Warning, Information, Error etc.
namespace CoreNLogRabbitMQ
{
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 CurrentClassLogger through LogManager of Nlog, i.e., logger.
Initialize an instance of LogEventInfo with log level type, a logger name, and a message.
using NLog;
using System;
using System.Collections.Generic;
namespace CoreNLogRabbitMQ
{
public class LogNLog : ILog
{
private static ILogger logger = LogManager.GetCurrentClassLogger();
public LogNLog()
{
}
public void Debug(string message)
{
Logger logger = LogManager.GetLogger("RmqTarget");
var logEventInfo = new LogEventInfo(LogLevel.Debug, "RmqLogMessage", $"{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 a name like blog. 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 as LogNLog.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using System;
using System.IO;
namespace CoreNLogRabbitMQ
{
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();
}
}
}
In nlog.config, we need to configure RabbitMQ. As we are going to use local RabbitMQ, set "localhost" as the Host, "guest" as username and password, and provide the exchange name which will be created in RabbitMQ if it's not there already.




Join the conversation! Your thoughts help the community grow.