step 1. right click on the project to select the Manage NuGet Package For Solution...
type NLog in the search Box to get the NLog Package.
step 2. after the installation completed. the NLog is automatically the NLog.Config File to the project.
configure the setting in the configuration file. The Trick is that the value of Name attibute in the Target Sections must match with the value of writeTo attribute in the Rules sections.
I already highlighted those two attributes in bold.
<targets>
<!-- add your targets here -->
<target name="ErrorLogs" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:inner=${newline}${exception:format=tostring,StackTrace:separator=*}${newline}}${newline}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" writeTo="ErrorLogs" />
</rules>
step 3. instantiate the Nlog Object to use in the page or class.
the following snippet of code showing i apply the NLog in the global.ascx object
private static readonly Logger logger = LogManager.GetLogger(typeof(Global).FullName);
protected void Application_Error(object sender, EventArgs e)
{
logger.Error("Unhandled exception.", Server.GetLastError());
Server.ClearError();
Server.Transfer(string.Format("~/Pages/Default.aspx?{0}", TrimQueryString()));
}
No comments:
Post a Comment