Skip to main content

How to Use Nlog with .Net Core 2.0 Application

1. Introduction

Logging is heart of the application. logging is very important for debugging and troubleshooting as well as for smoothness of application.

with the help of logging we can have end-to-end visibility for on-premise systems, to only give a fraction of that visibility for cloud-based systems. It can be difficult to instrument the cloud and, thus, alternative approaches, like logging, are required to give visibility into cloud-based components which otherwise can become black boxes from a performance or system monitoring perspective.

2. Install Nuget Package For nlog 

To use Nlog logging you need to first add Nlog pluging for adding pluging you can do it by different way.
1) Manage Nuget Packages.
2) Nuget command.

you can find nuget command for Nlog on mentioned link : Click Here

3. Create and add Nlog config file

we need to create Nlog.config file at root of project were we can keep nlog related configration.

Example :





For more detail Please click on mentioned link : Click Here

4. Update Startup file.

we need to load nlog related configration in relvent startup file. please refer below code.
NLog.GlobalDiagnosticsContext.Set("defaultConnection", Connection string);
   NLog.LogManager.LoadConfiguration(env.ContentRootPath + "\\NLog.config");

5. Custom Nlog Manager.

Manager Class :

public static class NLogManager {

 public static ILogger _logger = NLog.LogManager.GetCurrentClassLogger();

 public static void InfoLog(NLogData nLogData) {
  LogEventInfo theEvent = new LogEventInfo(LogLevel.Info, NLogManager._logger.Name, nLogData.Message);
  SetLogEventInfo(theEvent, nLogData);

 public static void DebugLog(NLogData nLogData) {
  LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, NLogManager._logger.Name, nLogData.Message);
  SetLogEventInfo(theEvent, nLogData);

 public static void ErrorLog(NLogData nLogData) {
  LogEventInfo theEvent = new LogEventInfo(LogLevel.Error, NLogManager._logger.Name, nLogData.Message);
  SetLogEventInfo(theEvent, nLogData);

Custom Event parameter for logging :

private static void SetLogEventInfo(LogEventInfo theEvent, NLogData nLogData) {
 theEvent.Properties["SessionId"] = nLogData.SessionId;
 theEvent.Properties["BrowserDetail"] = nLogData.BrowserDetail;

if you need to log more parameter into database just add property in above method and same add into Nlog.config file.

Model for NLog logging.

public class NLogData {
  public string SessionId {
  public string BrowserDetail {

Happy Coding. Please Add comment if you have any query. 


Popular posts from this blog

Custom AuthorizeAttribute in ASP.NET Core.

Introduction. Filter in MVC are attribute which can be apply to controller and action method. filters allow us to write our custom code before and after method execution. filters can be implemented into different level of scope Global, Class, and Method level. MVC provides different types of filters. The Authorization filter will run first and are used to determine whether the current user is authorized or unauthorized for the current request. In this article we will learn how to create custom authorization filter, were you can write your own authorization framework. Description. For creating Authorization filter IAuthorizationFilter interface must be inherited. For example, the following code demonstrate custom Authorization below CustomAuthorization attribute allow only Home controller to access the particular method under home controller class else it will return "error" in response if it is called via ajaxcall, otherwise it will be redirect to Sessi

How to use Log4Net in .Net Core 2.0 Application.

1. Introduction Logging frameworks are important because they make it easy to write your logs to different places by simply changing your configuration. You can write your .NET logs to a file on disk, a database, a log management system 2. Install Nuget Package For nlog  To use Nlog logging you need to first add Nlog pluging for adding pluging you can do it by different way. 1) Manage Nuget Packages. 2) Nuget command. you can find nuget command for Log4Net below. PM> Install-Package log4net -Version 2.0.8 3. Add log4net.config file We need to Add New to your project with name log4net.config Refer below code for log4net.config for logging log into file. Root is neccesary in log4net.config, in which we can define log level and appender-ref in which we can define appender for eg. FileAppender, ConsoleAppender. Layout : In layout we can define custom parameter sh

Enabling FxCop Code Analysis on .NET Core

Enabling FxCop Code Analysis on .NET Core. Description :- Code Analysis is important Because it is helpful from many aspects like Code Quality, Code Correctness, Code maintainability will remain great code for long time. Step 1.  Install Nuget Package : Microsoft.CodeAnalysis.FxCopAnalyzers   Click Here  to install Nuget. Refer Below screen shot additional packages will be installed after installing. Step 2. File with name FxCop.ruleset will be created in solution when you try to change action by right clicking on rule in visual studio as shown below. where you can find list of rule to be configured. If you have multi layer Architecture and you want to use similar ruleset for all layers then move ruleset file at parent level of project and add reference of that ruleset file to each project were we need to add FxCop rules. Ruleset path can be set at csproj level as below. I have added CodeAnalysisRuleSet within PropertyGroup with relevant path of code analysis r