Purnima Naik
Purnima Naik

Reputation: 2683

Adding properties to log message in NLog in .net core

I am using NLog for logging messages in .net core.

I have added NLog in StartUp.cs as follows:

loggerFactory.AddNLog();

For logging to file, I am using following method:

logger.LogInformation("Message");

I want to add custom NLog event properties to my message. But LogInformation() is not allowing me to pass that. How can I do that?

Upvotes: 8

Views: 18656

Answers (3)

truemedia
truemedia

Reputation: 1062

If you want custom layout properties (NLog calls them layout renderers) you can use the EventProperties Layout Renderer. You can simply do this in your code:

var logger = LogManager.GetCurrentClassLogger();
var eventInfo = new LogEventInfo(LogLevel.Info, logger.Name, "Message");
eventInfo.Properties["CustomValue"] = "My custom string";
eventInfo.Properties["CustomDateTimeValue"] = new DateTime(2020, 10, 30, 11, 26, 50);
// You can also add them like this:
eventInfo.Properties.Add("CustomNumber", 42);
// Send to Log
logger.Log(eventInfo);

Then you will be able to add these (any any properties you make up) in your nlog.config

<target>
  <parameter name="@customtime" layout="${event-properties:CustomDateTimeValue:format=yyyy-MM-dd HH\:mm\:ss}" />
  <parameter name="@customvalue" layout="${event-properties:item=CustomValue}" />
  <parameter name="@customnumber" layout="${event-properties:item=CustomNumber}" />
</target>

When using NLog with AspNetCore, it's useful to add the NLog.Web Package for ASP.NET Core which gives you many predefined Layout renderers. You can find more about NLog.Web for AspNetCore on their Github page.

This AspNetCore package will give you things like the following:

<parameter name="@UserName" layout="${aspnet-user-identity}" />
<parameter name="@MvcAction" layout="${aspnet-MVC-Action}" />
<parameter name="@Session" layout="${aspnet-session:Variable=User}" />
... etc

You will find the complete list included in available Options from NLog.Web.AspNetCore.

Upvotes: 12

Rolf Kristensen
Rolf Kristensen

Reputation: 19912

NLog can integrate with Microsoft ILogger, and will capture messsage-template properties:

logger.LogInformation("Message {PropertyName}", "PropertyValue");

It has been supported since NLog.Extensions.Logging ver. 1.0 in combination with NLog ver. 4.5.

More advanced examples of working with Microsoft ILogger can be found here: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-properties-with-Microsoft-Extension-Logging

Notice that one is not required to use Microsoft ILogger when developing a NetCore-application. NLog Logger ver. 4.5 works without any issues on both .NetFramework- and .NetCore-applications. See also https://github.com/NLog/NLog/wiki/How-to-use-structured-logging

Upvotes: 3

Ankit Mori
Ankit Mori

Reputation: 775

Find step by step information to setup Nlog in your .NET Application.

Step 1:- Installation

Install NLog and NLog.Extended of version 4.0.0.0 from Nuget Manager.

Step 2:- WebConfig configuration

    <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    </configSections>
    
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogFile="D:\NLogErrors\log.txt">
        <extensions>
          <!-- load NLog.Extended to enable ASP.NET-specific functionality -->
          <add assembly="NLog.Extended" />
        </extensions>
        <!--Define Various Log Targets like files, database or asp.net trace files-->
        <targets>
          <target name="oracle" xsi:type="Database" keepConnection="false" dbProvider="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionString="Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = "" )(PORT = 1521))(CONNECT_DATA =(SID = "")));User Id="";Password="";" commandText="INSERT INTO LOGTABLE (LOGMESSAGE,LOGLEVEL,LOGGERNAME,CREATEDDATETIME,SESSIONID,BROWSERDETAIL,REQUESTURL,ERRORMESSAGE,PRODUCERCODE,QUOTENO,CUSTOMER_IP_ADDRESS,SERVER_IP_ADDRESS) values(:LOGMESSAGE,:LOGLEVEL,:LOGGERNAME,:CREATEDDATETIME,:SESSIONID,:BROWSERDETAIL,:REQUESTURL,:ERRORMESSAGE,:PRODUCERCODE,:QUOTENO,:CUSTOMER_IP_ADDRESS,:SERVER_IP_ADDRESS)">
            <parameter name="LOGMESSAGE" layout="${message}" />
            <parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
            <parameter name="LOGGERNAME" layout="${logger}" />
            <parameter name="CREATEDDATETIME" layout="${date}" />
            <parameter name="SESSIONID" layout="${event-context:item=SessionId}" />
            <parameter name="BROWSERDETAIL" layout="${event-context:item=BrowserDetail}" />
            <parameter name="REQUESTURL" layout="${event-context:item=RequestUrl}" />
            <parameter name="ERRORMESSAGE" layout="${event-context:item=ErrorMessage}" />
            <parameter name="PRODUCERCODE" layout="${event-context:item=ProducerCode}" />
            <parameter name="QUOTENO" layout="${event-context:item=QuoteNo}" />
            <parameter name="CUSTOMER_IP_ADDRESS" layout="${event-context:item=CUSTOMER_IP_ADDRESS}"/>
            <parameter name="SERVER_IP_ADDRESS" layout="${event-context:item=SERVER_IP_ADDRESS}"/>
          </target>
          <target xsi:type="Mail" name="Email" html="true" addNewLines="false" replaceNewlineWithBrTagInHtml="false" subject="Error Log" to="[email protected]" useSystemNetMailSettings="true" body="${event-context:item=EmailBody}">
          </target>
        </targets>
        <rules>
          <logger name="*" minlevel="trace" writeTo="oracle" />
          
        </rules>
      </nlog>

Step 3:- Cs Code

    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);
                _logger.Log(theEvent);
            }
    }
    
     private static void SetLogEventInfo(LogEventInfo theEvent, NLogData 
         nLogData)
            {
                theEvent.Properties["SessionId"] = nLogData.SessionId;
                theEvent.Properties["BrowserDetail"] = nLogData.BrowserDetail;
                theEvent.Properties["RequestUrl"] = nLogData.RequestUrl;
                theEvent.Properties["ErrorMessage"] = nLogData.ErrorMessage;
                theEvent.Properties["EmailBody"] = nLogData.EmailBody;
                theEvent.Properties["ProducerCode"] = nLogData.ProducerCode;
                theEvent.Properties["QuoteNo"] = nLogData.QuoteNo;
                theEvent.Properties["CUSTOMER_IP_ADDRESS"] = nLogData.CustomerIPAddress;
                theEvent.Properties["SERVER_IP_ADDRESS"] = nLogData.ServerIPAddress;
            }

Step 4:- Model Entity


    public class NLogData
        {
            public string SessionId { get; set; }
    
            public string Message { get; set; }
    
            public string RequestUrl { get; set; }
    
            public string BrowserDetail { get; set; }
    
            public string Method { get; set; }
    
            public string ErrorMessage { get; set; }
    
            public string EmailBody { get; set; }
    
            public string ProducerCode { get; set; }
    
            public string QuoteNo { get; set; }
    
            public string CustomerIPAddress { get; set; }
    
            public string ServerIPAddress { get; set; }
        }

Step 5 :- Add Below line from where you have implement logging


     NLogManager.TraceLog(NLogData nlogData);

-- Create proper format which is accepted by above method and set value accordingly

If you have any query please leave comment or mail me on [email protected]

Upvotes: 1

Related Questions