
Introduction
Logging is the heart of an application. It is very important for debugging and troubleshooting, as well as, for smoothness of the 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.
Install NuGet Package For Nlog
To use Nlog logging, you need to first add the Nlog plugin. For adding the plugin, you use these two different ways.
- NuGet Package Manager.
- NuGet command.
You can find the Nuget command for Nlog on the mentioned link - Click Here
Create and add Nlog config file
We need to create the Nlog.config file at the root of the project where we can keep the Nlog related configuration.
Example
- <?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="false"
- throwConfigExceptions="true"
- internalLogLevel="Off"
- internalLogFile="c:\temp\internal-nlog.txt">
- <targets>
- <!-- write logs to file -->
- <target name="file" xsi:type="File" layout="
- -----------Time Stamp: ${longdate}----------
- Log Level: ${level}${newline}
- Logger Name : ${logger}${newline}
- Log Message : ${message}${newline}
- Exception Message: ${event-context:item=ErrorMessage}${newline}
- Browser Detail: ${event-context:item=BrowserDetail}${newline}
- Session Id: ${event-context:item=SessionId}
- fileName="${basedir}/logs/${shortdate}.log" />
- </targets>
- <rules>
- <logger name="*" minlevel="Error" writeTo="file" enabled="true" />
- <logger name="*" minlevel="Trace" writeTo="database" enabled="true"/>
- <logger name="Microsoft.*" maxlevel="Info" final="true" />
- </rules>
- </nlog>

Join the conversation! Your thoughts help the community grow.