Quick start: ASP.Net Core 3.1, Entity Framework Core, CQRS, React JS Series — Part 3: Custom logging configuration

Ali Süleyman TOPUZ
.Net Programming
Published in
2 min readDec 29, 2020
Well organized pieces make it understandable and readable!

The parent content of this series: Quick start: ASP.Net Core 3.1, Entity Framework Core, CQRS, React JS Series

In this content, a custom NLog configuration will be demonstrated.

Outline

  • Project Logical separation
  • NLog package installation and NLog.config file
  • LoggerManager and LoggerServiceExtensions
  • Startup

Project Logical separation

Separate the project as follows.

Domain can give reference but can’t take reference. That is pure definition of the entities and domain definitions.

NLog package installation and NLog.config file

dotnet add package NLog.Extensions.Logging

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="${specialfolder:folder=ApplicationData}/CWA/NLog.log">
<targets>
<target name="logfile" xsi:type="File"
fileName="${specialfolder:folder=ApplicationData}/CWA/Site.log"
layout="${longdate} ${level:uppercase=true} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>

In the configuration, it creates folder under the AppData folder and keeps the logs in that folder.

${specialfolder:folder=ApplicationData}

LoggerManager and LoggerServiceExtensions

using Domain.Infrastructure.Logging;
using NLog;

namespace Domain.Infrastructure.LoggerService
{
public class LoggerManager : ILoggerManager
{
private static ILogger logger = LogManager.GetCurrentClassLogger();
public void LogDebug(string message)
{
logger.Debug(message);
}
public void LogError(string message)
{
logger.Error(message);
}
public void LogInfo(string message)
{
logger.Info(message);
}
public void LogWarn(string message)
{
logger.Warn(message);
}
}
}
namespace Domain.Infrastructure.LoggerService
{
public static class LoggerServiceExtensions
{
public static void LoadConfiguration()
{
LogManager.LoadConfiguration(String.Concat(Directory.GetCurrentDirectory(), "/nlog.config"));
}

public static void ConfigureLoggerService(this IServiceCollection services)
{
services.AddSingleton<ILoggerManager, LoggerManager>();
}
}
}

Startup

public class Startup
{
public Startup(IConfiguration configuration)
{
LoggerServiceExtensions.LoadConfiguration();
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.ConfigureCors();
services.ConfigureIISIntegration();
services.ConfigureLoggerService();
services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseCors("CorsPolicy");
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All
});

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

Github feature branch

Conclusion

Now, NLog configuration is in placed.

--

--

Ali Süleyman TOPUZ
.Net Programming

Software Engineering and Development Professional. Writes about software development & tech. 📍🇹🇷