Featured post

Logging in ASP.NET Core 3.1 Using NLog

 

NLog  

     NLog is an open source logging framework that provides a great flexibility and configurable options to log the insights of your application. It allows to select multiple targets like database, files, console, etc. at the same time so that user don’t have to maintain different configurations in code. This is the most widely used logging framework out there.

 For more details : https://nlog-project.org/

   It is easy to implement in your Asp.Net core application. We'll see step by step with image.

  

1.   Open Visual Studio and Create new project




2.  Choose ASP.Net Core Web Application



3. Enter Name of project



4. Choose Web Application



 5. Right Click on your project name and choose Manage Nudget packages.
        Search these two nudget packages and install.

          1. NLog.Config
        2. NLog.Web.AspNetCore



             

    6. Open Nlog.config file and paste the code. 


<?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"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
 
  <targets>
    <target xsi:type="File" name="firstTarget" filename="log-${shortdate}.log"></target>
  </targets>
 
  <rules>
    <logger name="*" minlevel="Trace" writeTo="firstTarget" />
  </rules>
</nlog>

  

  7.  Paste the code in Program.cs file 

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddDebug();
                    logging.AddNLog();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });


 8 Press  F5  to  Run Your Project. 

after you ran your project you can see log in bin folder.


Comments

Popular posts from this blog

Hello World Program

What is JVM, JDK and JRE